Communicating between threads

Duncan Grisby dgrisby at uk.research.att.com
Tue Oct 2 06:00:51 EDT 2001


In article <mailman.1001703330.1355.python-list at python.org>,
 <fladak at rri.on.ca> wrote:

>Does anyone know how to communicate between threads?  It might help if I
>explained my application.
>
>I have two instances of an application, written in python, running.
>I refer to the application as a "viewer".  Both instances have two
>threads.  The main thread (I'll refer to it as thread A) handles the
>GUI events and the other thread (I'll refer to it as thread B)
>communicates to the other viewer using OmniOrb, an implementation of
>CORBA.  I'm trying to develop the code so that if you rotate the
>image in one viewer, the image in the other viewer rotates.

This is actually more a question of how to integrate a GUI toolkit
with multiple threads.

The answer all depends of the GUI toolkit you are using. Tkinter, for
example, is thread-safe, so you can just update the view from the
omniORB thread, and everything will work fine. You don't need to
involve the thread waiting for GUI events at all. You can see a simple
example of this in the gameClient.py file linked from here:

  http://www.uk.research.att.com/omniORB/omniORBpy/tutorial/

Unfortunately, despite the fact that threads make many aspects of GUI
programming much simpler, the majority of GUI toolkits aren't thread
safe. If you are using one like that, life is much harder. It's often
possible to provide an extra file descriptor to the GUI event loop's
select mechanism. That way, you can create a pipe for the event loop
to listen on. To wake up the GUI thread, you put a character into the
pipe and flush it. Then you can use normal thread synchronisation
mechanisms to tell the GUI thread what it has to do.

Cheers,

Duncan.

-- 
 -- Duncan Grisby  \  Research Engineer  --
  -- AT&T Laboratories Cambridge          --
   -- http://www.uk.research.att.com/~dpg1 --



More information about the Python-list mailing list