Communicating between threads

chris liechti cliechti at no.spam.gmx.net
Fri Sep 28 16:32:36 EDT 2001


fladak at rri.on.ca wrote in
news:mailman.1001703330.1355.python-list at python.org: 

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

Seems to be a clasical case for an "observer pattern". Each viewer 
registres itself at the subject, the image object in this case. If the 
image changes, it sends an update message to all registred viewers. That is 
in fact done by simply calling a method. (Such calls on objects shouldn't 
be any problem as CORBA handles all these things transparently localy or 
over network.)

I can send you a detailed example of that technique by email if you want.

> When I rotate the image in viewer 1, thread A in viewer 1 communicates
> to thread B in viewer 2 and requests a rotation.  Since thread A
> handles all the GUI events, I need some way to communicate this from
> thread B in viewer 2 to thread A in viewer 2.  
> 
> I can't use IPC or uni-directional named pipes here because thread A is
> already busy listening for events, so it can't listen on a pipe or a
> socket. 
> 
> I also tried using signals but experienced some problems in Linux and
> Windows. Under Linux, thread A which is the main thread registers the
> signal handler and if you send a signal from a thread other than the
> main thread (in this case thread B) the signal handler is not called. 
> You have to send the signal from the main thread to have the signal
> handler called.  Thus I can't use this as means to communicate between
> threads.  It seems that signals are meant as a way to communicate
> between processes not threads.  I wrote a test program where I sent a
> signal between threads and it didn't work.  Under Windows, the kill() 
> function which is used to send the signal just doesn't exist in python
> and many of the signals like SIGUSR1 don't exist.

A standard method for theads to communicate is to use Semaphores, Locks and 
Events. These classes are in the "threading" module and work well on Win 
and Linux.

Events are a perfect fit, when two threads have to be synchronized. One 
waits for the event and the other thread (e.g. that provides some data) can 
set the event when the data is ready.

Locks and Semaphores are used to protect data from multiple accesses from 
diffrent threads. Say one thread is updating a data structure, then an 
other thread must not access this data until thead one is finished with its 
work, otherwise thread two would read incomplete and wrong data.

I have also examples for that if you're inteseted.

> 
> Does anyone have any suggestions?  Thanks. 
> 
> -Faruq
> 
> ---------------------------------------------
> This message was sent using Endymion MailMan.
> http://www.endymion.com/products/mailman/
> 
> 
> 


-- 
chris <cliechti at gmx.net>




More information about the Python-list mailing list