Search code examples
pythonmultithreadingipc

Send a string to another python thread


I need to send strings to a separate thread asynchronously (meaning no one knows when). I am using threading because my problem is IO bound.

My current best bet is set a flag and have the receiver thread poll that. When it detects a set flag it reads from some shared memory.

This sounds horribly complicated. Can't I send messages via a mailbox? Or something even better like implicit variable semantics?


Solution

  • The queue.Queue class is ideal for this. Your thread can either block waiting for input, or it can check whether anything new has arrived, and it is thread-safe.