Because someone else will set it.

You generally start a thread in one part of your application and continue to do whatever you do:

thread = TimerClass()
thread.start()
# Do your stuff

The thread does it's stuff, while you do your stuff. If you want to terminate the thread you just call:

thread.event.set()

And the thread will stop.

So the answer is: event, in this case, is not used for controlling the thread from inside the thread object itself. It is used for controlling the thread from outside (from the object which holds the reference to the thread).

Answer from Viktor Kerkez on Stack Overflow
🌐
Python
docs.python.org › 3 › library › threading.html
threading — Thread-based parallelism
This is one of the simplest mechanisms for communication between threads: one thread signals an event and other threads wait for it.
Discussions

Equivalent of Python threading.Event for sync Rust
A simple and useful synchronization primitive in Python is threading.Event. It's basically a boolean flag that can be written to and read from an arbitrary number of threads, but it is also possible to block and wait with a timeout until the flag is set. I am trying to find a similar concurrency ... More on users.rust-lang.org
🌐 users.rust-lang.org
2
0
June 29, 2024
multithreading - Python threading: will Event.set() really notify every waiting thread - Stack Overflow
Or could it happen that those two lines are executed so quickly after each other, that some threads might still be waiting? (i.e. Event.wait() polls the event's state, which might be already "cleared" again) Thanks for your answers! ... In the internals of Python, an event is implemented with ... More on stackoverflow.com
🌐 stackoverflow.com
Any harm in idle threads?
now that i have some threading working, i’m building some simple logic and was wondering if leaving a thread to just idle in an if..then loop has any real negative performance cost? to whit, if the check fails, the thread does no work, but continues to check in the event the check succeeds. ... More on discuss.python.org
🌐 discuss.python.org
9
0
October 25, 2023
Python Threading Tutorial: Basic to Advanced (Multithreading, Pool Executors, Daemon, Lock, Events)
This video was pretty basic and did cover some of the topics in . However, some of your explanations were not quite correct. For example: Your explanation of what a thread was said that you needed them to share memory. This is not correct. Memory sharing is available to separate processes. Then you were saying that locks were needed to share memory. This is also not entirely true, and your example was what I would term naive: in that while it would work, it could cause bottlenecks for non trivial code. More on reddit.com
🌐 r/Python
10
192
November 4, 2024
🌐
Medium
dailybytes.medium.com › mastering-pythons-threading-event-cc0926ef6a88
Mastering Python’s threading.Event() | by Vivek | Medium
March 17, 2025 - threading.Event() is a built-in Python class that provides a simple way to synchronize threads.
🌐
Instructables
instructables.com › design › software
Starting and Stopping Python Threads With Events in Python Threading Module. : 4 Steps - Instructables
March 5, 2024 - Starting and Stopping Python Threads ... to start and stop threads using events. Threads are a way to execute multiple tasks concurrently within a single process Multithreading ......
🌐
Dkharazi
dkharazi.github.io › notes › py › threading › event
threading.Event
This is because Condition is an abstracted Event + Lock · >>> from threading import Thread, Event >>> import time >>> start_signal = Event() >>> def turtle(): ... print('go turtle') ... time.sleep(5) ... print('signal rabbit') ... start_signal.set() ... time.sleep(10) ...
Find elsewhere
🌐
Medium
rahulsreedharan.medium.com › seamless-multithreading-with-python-events-a-complete-guide-a1c5a3457e26
Seamless Multithreading with Python Events: A Complete Guide | by RSDevX | Medium
March 7, 2024 - Seamless Multithreading with Python Events: A Complete Guide In Python, events typically refer to synchronization primitives provided by the threading module for coordinating communication between …
🌐
Python Tutorial
pythontutorial.net › home › python concurrency › python threading event
Python Threading Event
June 3, 2023 - In this tutorial, you'll learn how to use the Python threading Event object to communicate between threads.
Top answer
1 of 3
17

In the internals of Python, an event is implemented with a Condition() object.

When calling the event.set() method, the notify_all() of the condition is called (after getting the lock to be sure to be not interrupted), then all the threads receive the notification (the lock is released only when all the threads are notified), so you can be sure that all the threads will effectively be notified.

Now, clearing the event just after the notification is not a problem.... until you do not want to check the event value in the waiting threads with an event.is_set(), but you only need this kind of check if you were waiting with a timeout.

Examples :

pseudocode that works :

#in main thread
event = Event()
thread1(event)
thread2(event)
...
event.set()
event.clear()

#in thread code
...
event.wait()
#do the stuff

pseudocode that may not work :

#in main thread
event = Event()
thread1(event)
thread2(event)
...
event.set()
event.clear()

#in thread code
...
while not event.is_set():
   event.wait(timeout_value)
#do the stuff

Edited : in python >= 2.7 you can still wait for an event with a timeout and be sure of the state of the event :

event_state = event.wait(timeout)
while not event_state:
    event_state = event.wait(timeout)
2 of 3
16

It's easy enough to verify that things work as expected (Note: this is Python 2 code, which will need adapting for Python 3):

import threading

e = threading.Event()
threads = []

def runner():
    tname = threading.current_thread().name
    print 'Thread waiting for event: %s' % tname
    e.wait()
    print 'Thread got event: %s' % tname

for t in range(100):
    t = threading.Thread(target=runner)
    threads.append(t)
    t.start()

raw_input('Press enter to set and clear the event:')
e.set()
e.clear()
for t in threads:
    t.join()
print 'All done.'

If you run the above script and it terminates, all should be well :-) Notice that a hundred threads are waiting for the event to be set; it's set and cleared straight away; all threads should see this and should terminate (though not in any definite order, and the "All done" can be printed anywhere after the "Press enter" prompt, not just at the very end.

🌐
GeeksforGeeks
geeksforgeeks.org › python › implement-inter-thread-communication-with-event-method-in-python
Implement Inter Thread Communication with Event( ) Method in Python - GeeksforGeeks
November 26, 2022 - In simple words sometimes one thread may be required to communicate to another thread depending on the requirements. This is considered as Inter Thread Communication. Event() Method: Here we talk about the Event() method, the Event object is considered or recommended as the simplest communication process or system between any threads.
🌐
Real Python
realpython.com › intro-to-python-threading
An Intro to Threading in Python – Real Python
August 5, 2024 - Technically, this example won’t have a race condition because x is local to inc(). It does illustrate how a thread can be interrupted during a single Python operation, however. The same LOAD, MODIFY, STORE set of operations also happens on global and shared values. You can explore with the dis module and prove that yourself. It’s rare to get a race condition like this to occur, but remember that an infrequent event taken over millions of iterations becomes likely to happen.
🌐
Reddit
reddit.com › r/python › python threading tutorial: basic to advanced (multithreading, pool executors, daemon, lock, events)
r/Python on Reddit: Python Threading Tutorial: Basic to Advanced (Multithreading, Pool Executors, Daemon, Lock, Events)
November 4, 2024 -

Are you trying to make your code run faster? In this video, we will be taking a deep dive into python threads from basic to advanced concepts so that you can take advantage of parallelism and concurrency to speed up your program.

  • Python Thread without join()

  • Python Thread with join()

  • Python Thread with Input Arguments

  • Python Multithreading

  • Python Daemon Threads

  • Python Thread with Synchronization using Locks

  • Python Thread Queue Communication between Threads

  • Python Thread Pool Executor

  • Python Thread Events

  • Speed Comparison I/O Task

  • Speed Comparison CPU Task (Multithreading vs Multiprocessing)

https://youtu.be/Rm9Pic2rpAQ

🌐
Super Fast Python
superfastpython.com › home › tutorials › threading event object in python
Threading Event Object In Python - Super Fast Python
September 11, 2022 - You can use an Event Object in Python via the threading.Event class. In this tutorial you will discover how to use an event object in Python. Let’s get started. Need for an Event Object A thread is a thread of execution in a computer program. Every Python program has at least one thread of execution […]
🌐
Reddit
reddit.com › r/coding › how to use threading.event object in python to control the flow of thread execution
r/coding on Reddit: How to use threading.Event object in Python to control the flow of thread execution
June 17, 2024 - Threads things · r/dontyouknowwhoiam • · r/dontyouknowwhoiam · This is a place for instances of people not realizing who they're talking to is who they're talking about. Members Online · upvotes · · comments · I never knew events were THIS powerful - A Python observer pattern tutorial ·
🌐
YouTube
youtube.com › watch
Python Tutorials: Threading Beginners Tutorial -Event Objects - part 7 - YouTube
This video is part of Python Threading Beginners Tutorial playlist.This is not the first video of the playlist. If you are new to threads please start the p...
Published   July 9, 2017
🌐
Reddit
reddit.com › r/learnpython › is there any reason to use thread in python?
r/learnpython on Reddit: is there any reason to use thread in python?
October 11, 2023 -

after learning more about asyncio (for IO bound) and multiprocessing (for CPU bound), I start to wonder if there is any real life use case for threading in python. It seems python threading comes with its own baggage, like GIL lock, and it was mentioned that python threading, is not really really multi-thread since due to limitation on interpreters' binding to CPU, only a thread could be scheduled at a time?

This makes me wonder why we even want to use python thread at all?

Top answer
1 of 9
18
Here is a real world example: If you have a blocking call, like requests library, making a call to an api with a long latency, in a GUI application. When making the call the GUI locks up until requests returns. If you run the call to requests in a separate thread, the GUI is not locked.
2 of 9
9
Threading is absolutely not legacy and both threading and asyncio have their place. Asyncio and threading work similarly at the high level but don't work the same at the low level. At the low level threads are still managed by the operating system, where asyncio tasks are managed with a Python event loop. With threading the OS can change threads at ANY time during execution, so you have to be more careful about how data is accessed. With asyncio you choose when work between tasks can change. Asyncio is great for LOTS of tasks, especially since it has less overhead. Threading has more overhead and having lots of threads can start to slow things down more than it speeds up. One of the subtle issues with asyncio is if a task gets hung up or doesn't play nice it can prevent all your other tasks from running. For critical tasks that absolutely must keep running threading is better. For example I often write interfaces that receive critical telemetry from something over a UDP port. The front end consists of a thread that receives data on the UDP socket then logs it and puts it in a queue. Another processing thread then pulls the data from the queue to process it. The receiving and logging is super critical. If the processing thread dies or gets hung up it's not a huge deal as long as the data is still being received and logged.
🌐
Reddit
reddit.com › r/learnpython › why does "event.set()" not break a "while not event.is_set()" loop in a sub-thread? (see code)
Why does "event.set()" not break a "while not event.is_set ...
August 5, 2022 -

[SOLVED: use a queue with a poison pill instead of events]

I'm using events to communicate between my main code and a sub-thread. The thread contains a while loop that should get exited once stop_event.set() is called in the main code:

import time
from threading import *

def stream_data(stop_event, new_image_event):
    while not stop_event.is_set():
        new_image_event.wait()
        new_image_event.clear()

        # Do some stuff
        print('Stuff!')

    print('Successfully exited while loop!')
    return 


if __name__ == '__main__':

    # Initialising events
    stop_event = Event()
    new_image_event = Event()

    # Starting the thread
    thread = Thread(target=stream_data, args=(stop_event, new_image_event, ))
    thread.start()

    # Do stuff before setting the stop event, while generating a 'new' new_image_event every 1s
    for i in range(5):
        new_image_event.set()
        time.sleep(1)

    # generating the stop event
    stop_event.set()
    print('stop_event has been set')

    # joining back the thread
    thread.join()

Output:

Stuff!
Stuff!
Stuff!
Stuff!
Stuff!
stop_event has been set

So the new_image_event does it's job, and at the end the stop_event is successfully set. But for some reason this does not break the while loop in the thread, and 'Successfully exited while loop!' is never printed. Why? And how can I solve this (preferably without resorting to classes and/or self)?

🌐
Studytonight
studytonight.com › python › python-threading-event-object
Python Event Object | Studytonight
This class is used for inter thread communication by generating events. The Event class object provides a simple mechanism which is used for communication between threads where one thread signals an event while the other threads wait for it.