Inter-Thread Communication

Dale Strickland-Clark dale at riverhall.NOTHANKS.co.uk
Thu Nov 15 17:39:44 CET 2001


I have a master thread and a number of subordinate threads. They all
need to communicate between each other synchronously - in an OS
independent way.

A typical exchange might be:

T-A waits for a message from somewhere.
T-B sends T-A a message about an event and waits for a response.
T-A deals with the event and sends a response to T-B.
T-A waits for another message.

Pretty standard stuff, I would have thought. However, I can't see a
tidy way of doing this in Python that doesn't involve three locks:

The three locks, all of type threading.Lock, are:

Message - global
Post - global
Response - local to thread

 1. T-A acquires Message lock
 2. T-B acquires Response lock.
 3. T-A tries to acquires Message lock - blocking
 4. T-B wants to post a message so acquires Post lock - blocking.
 5. T-B get's Post lock and stores message details.
 6. T-B releases Message lock - waking T-A.
 7. T-B tries to acquire Response lock - blocking
 8. T-A get's Message lock, handles message and stores response.
 9. T-A releases Response lock - waking T-B
10. T-A releases Post lock ready for another thread
11. continue from 2

This seems awfully fussy to me. Is there an easier way?
--
Dale Strickland-Clark
Riverhall Systems Ltd



More information about the Python-list mailing list