Problem: To know when will a launched thread actually starts running.
A key feature of threads is that they execute independently and nondeterministically. This can present a tricky synchronization problem if other threads in the program need to know if a thread has reached a certain point in its execution before carrying out further operations. To solve such problems, use the Event object from the threading library.
Event instances are similar to a “sticky” flag that allows threads to wait for something to happen. Initially, an event is set to 0. If the event is unset and a thread waits on the event, it will block (i.e., go to sleep) until the event gets set. A thread that sets the event will wake up all of the threads that happen to be waiting (if any). If a thread waits on an event that has already been set, it merely moves on, continuing to execute.
Code #1 : Code that uses an Event to coordinate the startup of a thread.
On running the code above, the “countdown is running” message will always appear after the “countdown starting” message. This is coordinated by the event that makes the main thread wait until the
countdown() function has first printed the startup message.
- Event objects are best used for one-time events. That is, create an event, threads wait for the event to be set, and once set, the Event is discarded.
- Although it is possible to clear an event using its
clear()method, safely clearing an event and waiting for it to be set again is tricky to coordinate, and can lead to missed events, deadlock, or other problems (in particular, it can’t be guaranteed that a request to clear an event after setting it will execute before a released thread cycles back to wait on the event again).
- If a thread is going to repeatedly signal an event over and over, it’s probably better off using a Condition object instead.
Code #2 : Implementing a periodic timer that other threads can monitor to see whenever the timer expires.
Code #3 : Use of timer
A critical feature of Event objects is that they wake all waiting threads. If writing a program where it is only desired to wake up a single waiting thread, it is probably better to use a Semaphore or Condition object instead.
Code #4 : Code involving semaphores
On running this code, a pool of threads will start, but nothing happens because they’re all blocked waiting to acquire the semaphore. Each time the semaphore is released, only one worker will wake up and run as shown in the code below
Code #5 :
Working 0 Working 1
Writing code that involves a lot of tricky synchronization between threads is likely to make your head explode. A more sane approach is to thread threads as communicating tasks using queues or as actors.
- Getting Started With Testing in Python
- Start and stop a thread in Python
- Python - Getting all the Wifi Devices the system has connected
- Grammar Checker in Python using Language-check
- Python program to check if the given string is IPv4 or IPv6 or Invalid
- Check CBSE result using Selenium in Python
- Python Script to check PC last reboot time
- Python | Add Logging to a Python Script
- Python | Add Logging to Python Libraries
- twitter-text-python (ttp) module - Python
- Creating and updating PowerPoint Presentations in Python using python - pptx
- XML parsing in Python
- Twitter Sentiment Analysis using Python
- pprint : Data pretty printer in Python
- Desktop Notifier in Python
- Junk File Organizer in Python
- Tracking bird migration using Python-3
- Python Desktop News Notifier in 20 lines
- Send mail with attachment from your Gmail account using Python
- Hangman Game in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.