Discussion:
How to break while loop based on events raised in a thread (Python 2.7)
(too old to reply)
marc nicole
2024-11-27 12:28:06 UTC
Permalink
I am using the below class1 code to detect some event through the method

check_for_the_event()
# Some class1def check_for_the_event(self):
thread1 = threading.Timer(2, self.check_for_the_event)
thread1.start()# some processingif event is detected:
self.event_ok = Trueelse:
self.event_ok = False

then I pass an instance of that class to the below class to know when the
event is on or off and act upon its value accordingly using the following
below code:

# execution of other part of the program (where
self.another_class_instance.event_ok = False and
self.some_other_condition is true)
current_time = current_time_some_cond = current_max_time
= time.time()
while not self.another_class_instance.event_ok and
self.some_other_condition:
while self.some_other_condition and not
self.another_class_instance.event_ok:#self.some_other_condition takes
up to 10 secs to be checked (minimum 5 secs)
if time.time() > current_time + 5:
current_time = time.time()
# some processing
else:
while not self.another_class_instance.event_ok:
#processing
if time.time() > current_max_time + 60 * 2 and
not self.another_class_instance.event_ok:
#some other processing
if time.time() > current_time_some_cond + 10
and not self.cond1 and not self.another_class_instance.event_ok:
# some processing that takes 2-3 seconds
self.cond1 = True
current_time_some_cond = time.time()
elif self.cond1 and time.time() >
current_time_some_cond + 10 and not
self.another_class_instance.event_ok:
current_time_some_cond = time.time()
#some other processing that takes 2-3 seconds
else:
pass
else:
pass

The problem is the real time execution of the program (in class2) requires
an instant check to be performed and that cannot wait until the current
loop instructions finish, and I want is to break out of the outer while
loop if the event is on without further checking any other condition.

for now I perform multiple checks at each if or while statement, but is
there a IO async based method that breaks out of the loop when the event is
raised in the thread?
Stefan Ram
2024-11-27 14:19:25 UTC
Permalink
Subject: How to break while loop based on events raised in a
thread (Python 2.7)
`Threading.Event` is your go-to for thread communication without
breaking a sweat.

It's a synchronization primitive that lets threads chat with
each other using a simple flag mechanism. Think of it as a
traffic light for your threads.

Here's the lowdown:

Creating an event:

import threading
event = threading.Event()

. Checking if the event is set:

if event.is_set():
print( "Green light, go!" )

. Setting the event (turning the light green):

event.set()

. Clearing the event (back to red):

event.clear()

. Waiting for the event to be set:

event.wait() # Will chill here until the event is set

. Waiting with a timeout (for the impatient threads):

if event.wait(timeout=5):
print( "Event was set within 5 seconds" )
else:
print( "Timed out, event wasn't set" )

. The cool thing about `threading.Event` is that it's like a bouncer
at a club. One thread can be the bouncer (setting or clearing the
event), while other threads wait in line (using `wait()`). When the
bouncer gives the green light, all waiting threads get to party.

Here's a quick example to tie it all together:

import threading
import time

def waiter( event, name ):
print( f"{name} is waiting for the event" )
event.wait()
print( f"{name} received the event signal!" )

def main():
event = threading.Event()

# Create some threads that wait for the event
threading.Thread( target=waiter, args=( event, "Thread 1" )).start()
threading.Thread( target=waiter, args=( event, "Thread 2" )).start()

print( "Main thread: Chilling for 3 seconds before setting the event" )
time.sleep( 3 )

print( "Main thread: Setting the event!" )
event.set()

if __name__ == "__main__":
main()

. This script is like throwing a surprise party. The main
thread is setting up, while the other threads are waiting
outside. When everything's ready, the main thread yells
"Surprise!" (sets the event), and everyone rushes in.

Remember, `threading.Event` is your friend when you need
simple thread synchronization without the complexity of locks
or semaphores. It's perfect for those "wait until I say go"
scenarios in your multi-threaded Python fiesta.

Loading...