Inter-Thread Communication

Werner Schiendl ws-news at gmx.at
Thu Nov 15 12:32:39 EST 2001


Hi,

I think you can use the Queue module.
You than have a queue for each separate communication path.
The locking is built into the queue.

hth
Werner

"Dale Strickland-Clark" <dale at riverhall.NOTHANKS.co.uk> wrote in message
news:4go7vtc8dqm8qsfq8ttv95bopidscj56l2 at 4ax.com...
> 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