🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The threading.Event class in Python's threading module provides a simple mechanism for signaling between threads. It allows one or more threads to wait until they are notified that an event has occurred.
Table of Contents
- Introduction
threading.EventClass Syntax- Examples
- Basic Usage
- Using Event to Control Threads
- Using Event with Timeout
- Real-World Use Case
- Conclusion
Introduction
The threading.Event class is used to manage an internal flag that can be set to true with the set() method and reset to false with the clear() method. The wait() method blocks until the flag is set to true. This can be useful for coordinating the start and stop of threads or other synchronization needs.
threading.Event Class Syntax
Here is how you create and use an event with the threading.Event class:
import threading
event = threading.Event()
Methods:
set(): Set the internal flag to true. All threads waiting for it to become true are awakened. Threads that callwait()once the flag is true will not block.clear(): Reset the internal flag to false. Subsequently, threads callingwait()will block untilset()is called again.wait(timeout=None): Block until the internal flag is true. If the internal flag is true on entry, return immediately. If the internal flag is false, block until another thread callsset()or until the optional timeout occurs.is_set(): Return true if and only if the internal flag is true.
Examples
Basic Usage
Create and use an event to synchronize threads.
Example
import threading
import time
event = threading.Event()
def worker():
print("Worker waiting for event to be set")
event.wait()
print("Worker received event")
thread = threading.Thread(target=worker)
thread.start()
time.sleep(2)
print("Main thread setting event")
event.set()
thread.join()
Using Event to Control Threads
Use an event to start and stop a thread.
Example
import threading
import time
event = threading.Event()
def worker():
while not event.is_set():
print("Worker is running")
time.sleep(1)
print("Worker stopping")
thread = threading.Thread(target=worker)
thread.start()
time.sleep(5)
print("Main thread stopping worker")
event.set()
thread.join()
Using Event with Timeout
Use an event with a timeout to control how long a thread waits.
Example
import threading
import time
event = threading.Event()
def worker():
while not event.wait(timeout=2):
print("Worker is running")
print("Worker stopping")
thread = threading.Thread(target=worker)
thread.start()
time.sleep(5)
print("Main thread stopping worker")
event.set()
thread.join()
Real-World Use Case
Synchronizing Multiple Threads
Use events to synchronize multiple threads.
Example
import threading
import time
start_event = threading.Event()
stop_event = threading.Event()
def worker(worker_id):
start_event.wait()
while not stop_event.is_set():
print(f"Worker {worker_id} is running")
time.sleep(1)
print(f"Worker {worker_id} stopping")
threads = [threading.Thread(target=worker, args=(i,)) for i in range(3)]
for thread in threads:
thread.start()
time.sleep(2)
print("Main thread starting all workers")
start_event.set()
time.sleep(5)
print("Main thread stopping all workers")
stop_event.set()
for thread in threads:
thread.join()
Conclusion
The threading.Event class is used for managing signaling between threads in Python. It allows you to coordinate actions and synchronize the execution of multiple threads effectively. Proper usage can enhance the reliability and control of your multithreaded applications.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment