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!
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
> 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
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.