how to start a new thread from a GUI

Brian Kelley bkelley at wi.mit.edu
Thu Aug 16 15:40:26 EDT 2001


Apologies is this is received more than once.

>Quoth Wen Jiang <wj691781 at bcm.tmc.edu>:
>| I have a GUI to submit a background job and want the GUI to exit after
>| the job is submitted to leave the actually big job run for a few days to
>| compleletion. Could anyone give me a hint how to do this? right now the
>| GUI stays but blocked by the background job.
>| Thanks.
>
>os.spawnv(os.P_NOWAIT, cmd, argv) might work for you.
>
>	Donn Cave, donn at u.washington.edu
>
Which GUI toolkit are you using? There are several ways to solve this
problem but eventually you will find that you want to have asynchronous
I/O between the worker and the GUI.

If you are writing the process yourself then it is fairly easy to
accomplish, attached is a small piece of Tkinter code that can launch a
threaded task and still run the GUI. It also can stop the thread from 
running
using a simple message passing model between the gui thread and the
worker thread.

The message passing interface between the parent and the child is a
thread safe Queue. When this Queue is not empty, the worker thread will
shut down. You could also have a second thread to pass UI update events
to the Gui. Generally, I find that explaining Queues to the people I
work with much easier than thread Events and Semaphores but your milage
may vary. Queues are not reliable if you need less latency between user
events and visible effects. Also debugging becomes much harder as soon
as you start multithreading an application. Michael Swaine once said
that "Multi-threading rots your teeth"

Also be aware that whenever you use python's thread package with a
purely python written program you will see slowdowns in the workers you
are running. We have seen up to 30% speed loss between a threaded
process and an unthreaded process. If speed is an issue you can set up
the worker to run in a seperate python process and use pipes/sockets for
update and control events. Python makes this fairly easy to do, although
whenever you touch pipes/sockets you are getting into the realm of
techno-weenie. Let me know if you need advice on this, we are trying to
release some code to facilitate linux and windows out of process
asynchrounous communication, but don't hold your breath.

As an aside, this same technique works equally well under PyQT/Gtk and
Tkinter. I remember doing something similar under WxPython but I
remember them having their own threading model.


Brian Kelley
Whitehead Institute For Biomedical Research





-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: test_thread.py
URL: <http://mail.python.org/pipermail/python-list/attachments/20010816/f95f4989/attachment.ksh>


More information about the Python-list mailing list