Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Invoke a method to a specific thread

1 view
Skip to first unread message

Varghjärta

unread,
Jul 25, 2005, 1:20:39 AM7/25/05
to Python List
Taking the opportunity to ask yet another python question as a newbie
to this list, that's been bugging a wee but that I've managed to put
off.

When doing GUI apps in C# I often have to call a method that will
modify the GUI somehow from a different thread then the GUI is on (to
allow for GUI responsiveness). I simply call Invoke() or BeginInvoke
which resides in another thread with a delegate pointing to the method
I want to start in that thread.

How would I go about doing this in Python?

If I have "GUIClass":...
And start another thread and from that thread I want to call the
method "PaintSomething()" in "GUIClass" _in_ the same thread that
"GUIClass" lives in.

If I'm making no sense please tell me. This is something I'd really
want to know how to achieve.

Thanks in advance!

en.kar...@ospaz.ru

unread,
Jul 25, 2005, 1:31:10 AM7/25/05
to pytho...@python.org
Mon, Jul 25, 2005 at 01:20:39PM +0800, Varghj?rta пишет:

> When doing GUI apps in C# I often have to call a method that will
> modify the GUI somehow from a different thread then the GUI is on (to
> allow for GUI responsiveness). I simply call Invoke() or BeginInvoke
> which resides in another thread with a delegate pointing to the method
> I want to start in that thread.
>
> How would I go about doing this in Python?

I don't know of this functionality in the standard python library, but look
at the Twisted framework (http://twistedmatrix.com).

What you need is

http://twistedmatrix.com/projects/core/documentation/howto/threading.html#auto2

--
jk

Diez B.Roggisch

unread,
Jul 25, 2005, 7:03:08 AM7/25/05
to pytho...@python.org
Varghjärta <varghjarta <at> gmail.com> writes:

> If I have "GUIClass":...
> And start another thread and from that thread I want to call the
> method "PaintSomething()" in "GUIClass" _in_ the same thread that
> "GUIClass" lives in.

There is no such thing in python, or any programming language for that matter.
If a thread wants to run in an endless loop, there is nothing you can do about
that - apart from maybe killing it. But even that won 't work all the time and is
dangerous.

However, in GUIs there is usually some so called event-loop. And for the
task you have in mind you need a way to insert a custom event into that loop
that will be executed the next time that gui thread is idle.
Qt uses QCustomEvents for this. Swing has it's SwingUtilities.invokeLater

http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html#invokeLater

and others have different means. So - whatever GUI you use,
it depends on that what to do.

Diez

Varghjärta

unread,
Aug 4, 2005, 3:50:55 AM8/4/05
to Python List
On 25/07/05, Diez B.Roggisch <de...@web.de> wrote:
> Varghjärta <varghjarta <at> gmail.com> writes:
>
> > If I have "GUIClass":...
> > And start another thread and from that thread I want to call the
> > method "PaintSomething()" in "GUIClass" _in_ the same thread that
> > "GUIClass" lives in.
>
> There is no such thing in python, or any programming language for that matter.

Not exactly sure what you mean, what would you call the ability to
sort of, push a method call onto a thread like Invoke() in C# for
example.
Even if underneath the clean surface it posts the call into the
message loop it gets the job done.


> However, in GUIs there is usually some so called event-loop. And for the
> task you have in mind you need a way to insert a custom event into that loop
> that will be executed the next time that gui thread is idle.
> Qt uses QCustomEvents for this. Swing has it's SwingUtilities.invokeLater
>
> http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html#invokeLater
>
> and others have different means. So - whatever GUI you use,
> it depends on that what to do.


Yeah, I've been playing around with WX and find it to be the best GUI
layer for python, I belive it has some way of dealing with it.

But lets say i'm not using a GUI. Or rather, for example in my
WxPython application I have classes which needs to perform time
consuming operations. I would _really_ not want to include lots of
WX-stuff in an otherwise purely python class. I don't like mixing GUI
code with other code, it feels very dirty.

How would a python programmer(more experienced then I currently am) solve this?

Should I implement some kind of message-loop-system in the class which
the different threads "posts" to, and then the class(which is in the
right thread) deals with any events "posted"?

I'm spoiled from C# having everything delivered to me, so I'm confused
as to how much I need to implement myself. Thinking perhaps there was
some cute special python command I could use to do it for me :)

P.S. Currently traveling far from home(but still I have programming on
me brain), sorry for the delayed reply.

0 new messages