threading.Condition vs threading.Event
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Understanding `threading.Condition` vs `threading.Event` in Python
In Python's threading module, both `Condition` and `Event` objects are synchronization primitives used to control the execution of threads. They serve to coordinate threads so they can alter their execution paths based on conditions or events they need to wait for before proceeding. While their purpose may overlap, there are nuanced differences in how they achieve thread synchronization.
`threading.Condition`
A `Condition` object in Python is used to allow one or more threads to wait until notified by another thread. It combines a lock that can be used to control access to some shared resource and a set of methods, `wait()`, `notify()`, and `notify_all()`, which let threads communicate. Here are key points about `Condition`:
- Lock Integration: Each `Condition` uses a lock object; often a `threading.Lock` or `threading.RLock`. This lock must be acquired before any operations like `wait()`, `notify()`, or `notify_all()` can be performed on the condition object.
- Wait and Notify: Threads can call `wait()` to release the lock and block until another thread calls `notify()`, indicating that some condition has changed, or `notify_all()`, which awakens all waiting threads.
- Suitability: It's suitable for more complex state transitions situations, like when multiple conditions should be met before a thread can proceed.
Example of `threading.Condition`
- Internal Flag: `Event` maintains a boolean flag, initially set to `False`.
- Wait and Signal: Threads can call `wait()` and block until the flag is set to `True` by another thread. Once set, all those waiting threads proceed past their wait calls.
- Set and Clear: Any thread can `set()` the flag to `True`, `clear()` it back to `False`, or check its status using `is_set()`.
- Use Case: Ideal for scenarios where simply waiting for an external condition or signal to proceed is sufficient.
- Complex Task Coordination: Use `Condition` when you have multiple threads that depend on one another or when the coordination requires multiple stages.
- Simple Signaling: Choose `Event` when you only need threads to be aware of a single state crossing (like starting, stopping, pausing).
Related reading
- threading.Timer - repeat function every 'n' seconds
- ThreadPoolExecutor Block When its Queue Is Full?
- ThreadPoolExecutor with corePoolSize 0 should not execute tasks until task queue is full
- ThreadPool.QueueUserWorkItem vs Task.Factory.StartNew
- Threads is not executing in parallel python with ThreadPoolExecutor
- Threads vs Asynchronous Networking Twisted Python
- Threads and Reactive Programming in Ballerina
- Threads configuration based on no. of CPU-cores
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.