Mechanisms for Interthread Communication (ITC)
Learn two mechanisms for interthread communication in Python.
We'll cover the following
Python’s threading
module provides two mechanisms for interthread communication:
- Event
- Condition
Event
An Event
object is used to communicate between threads. It has an internal flag that threads can set or clear by using the set()
and clear()
methods.
Typical working
If thread 1 calls the wait()
method, it will wait (block) if the internal flag has not yet been set. Thread 2 will set the flag. Since the flag now stands set, thread 1 will come out of its wait state, perform its work, and then clear the flag. This scenario is shown in the following program:
Get hands-on with 1200+ tech skills courses.