Gui thread and async jobs.

K

king kikapu

Hi, i am reading the book "Python Cookbook, 2nd edition" and i
encountered a very handy recipe, the one that is called "Combining
GUIs and Asynchronous I/O with Threads"

It is talking about retain a main GUI thread, doing async work with
worker threads and have both talk through a Queue object to dispatch
their messages, so the main (GUI) thread remain responsive.
It has a very good example by using Tkinter and Qt that is indeed
working. The only point that make me wonder is that the QUI thread has
to use some polling to check for messages in the Queue.

Author said that another alternatives exists (by not doing polling)
but the complexity makes them non-practical for the 90% of ocassions.
I remember in C# we deal with that sort of things with events/
delegates.
Hos the same thing is possible in Python, has anyone any link(s) to
have a look ?
 
D

Diez B. Roggisch

king said:
Hi, i am reading the book "Python Cookbook, 2nd edition" and i
encountered a very handy recipe, the one that is called "Combining
GUIs and Asynchronous I/O with Threads"

It is talking about retain a main GUI thread, doing async work with
worker threads and have both talk through a Queue object to dispatch
their messages, so the main (GUI) thread remain responsive.
It has a very good example by using Tkinter and Qt that is indeed
working. The only point that make me wonder is that the QUI thread has
to use some polling to check for messages in the Queue.

Author said that another alternatives exists (by not doing polling)
but the complexity makes them non-practical for the 90% of ocassions.
I remember in C# we deal with that sort of things with events/
delegates.
Hos the same thing is possible in Python, has anyone any link(s) to
have a look ?

It depends on the toolkit you use. Qt has thread-safe custom events in 3.x,
and afaik signal/slots (and thus events) are generally thread-safe in 4.x.
So, no problems there.

Diez
 
K

king kikapu

It depends on the toolkit you use. Qt has thread-safe custom events in 3.x,
and afaik signal/slots (and thus events) are generally thread-safe in 4.x.
So, no problems there.

Diez

Aha...So you do not use polling there (in Qt), correct ?
 
J

Jarek Zgoda

king kikapu napisa³(a):
Hi, i am reading the book "Python Cookbook, 2nd edition" and i
encountered a very handy recipe, the one that is called "Combining
GUIs and Asynchronous I/O with Threads"

It is talking about retain a main GUI thread, doing async work with
worker threads and have both talk through a Queue object to dispatch
their messages, so the main (GUI) thread remain responsive.
It has a very good example by using Tkinter and Qt that is indeed
working. The only point that make me wonder is that the QUI thread has
to use some polling to check for messages in the Queue.

Author said that another alternatives exists (by not doing polling)
but the complexity makes them non-practical for the 90% of ocassions.
I remember in C# we deal with that sort of things with events/
delegates.
Hos the same thing is possible in Python, has anyone any link(s) to
have a look ?

Another option, although not a silver bullet, is to use a message
dispatcher, like Louie (http://www.pylouie.org/). Unfortunately, the
Louie dispatcher seems to be synchronous, so I wouldn't recommend it for
the environment with "high density" of events. If your application
dispatches a message then sits idle for some time, Louie will fit
perfectly as the queuing of messages will not happen. Otherwise there
would be no advantage other than code simplification. And this counts
always. :)
 
G

Grant Edwards

Hi, i am reading the book "Python Cookbook, 2nd edition" and i
encountered a very handy recipe, the one that is called
"Combining GUIs and Asynchronous I/O with Threads"

It is talking about retain a main GUI thread, doing async work
with worker threads and have both talk through a Queue object
to dispatch their messages, so the main (GUI) thread remain
responsive. It has a very good example by using Tkinter and Qt
that is indeed working. The only point that make me wonder is
that the QUI thread has to use some polling to check for
messages in the Queue.

It sounds to me like Qt is missing some rather important
features and/or convenience functions. In other toolkits (e.g.
wxPython), invoking GUI methods/functions from non-GUI threads
is trivial and involves no polling by the GUI thread.

For example, if in a wxPython application you wanted to call
someGUIobject.method(foo,bar) from a non-GUI thread you just do
this:

wx.CallAfter(someGUIobject.method,foo,bar)

If you look under the covers there is a queue involved, but
it's nothing the user has to poll or worry about.
 
P

Phil Thompson

It sounds to me like Qt is missing some rather important
features and/or convenience functions. In other toolkits (e.g.
wxPython), invoking GUI methods/functions from non-GUI threads
is trivial and involves no polling by the GUI thread.

For example, if in a wxPython application you wanted to call
someGUIobject.method(foo,bar) from a non-GUI thread you just do
this:

wx.CallAfter(someGUIobject.method,foo,bar)

If you look under the covers there is a queue involved, but
it's nothing the user has to poll or worry about.

In Qt it's all part of the signal/slot mechanism which works across threads.
Surprise, surprise, under the covers there is a queue involved.

Phil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top