how to exit a function when an event is set?
Question:
I have an infinite loop thread that sets an event when a sensor is high/true
event = threading.Event()
def eventSetter():
while True:
if sensor:
event.set()
else:
event.clear()
th1 = threading.thread(target=eventSetter)
th1.start
and I have a function capture that takes 5 sec to execute
def capture():
time.sleep(2) #sleep represents a task that takes 2 sec to finish
time.sleep(1)
time.sleep(2)
return
now I want to exit the function capture in the middle of its task whenever the event is set
for example if task 1 takes 5sec to finish and the event occurs at time 2sec, the task should not continue at time 2sec and the function should exit
I tried checking for the event every line but i don’t know how to exit in the middle of its task thus it waits for the task to finish before return applies also I didn’t like the look of multiple if/return
def capture():
time.sleep(2) #sleep represents a task that takes sec to finish
if event.is_set():
return
time.sleep(1)
if event.is_set():
return
time.sleep(2)
if event.is_set():
return
Answers:
Do you want breaking loop ?
if you want break any time or condition you need to break
`key.
for example
event = threading.Event()
def eventSetter():
while True:
if sensor:
event.set()
break
#loop stoped.
else:
event.clear()
return True #or none.
You’re going to have to check the event state every chance you get in order to break out of capture
as soon as possible. The variation you provided is fine, but here are a couple other ways to do the same thing:
def capture():
# nested checks are fine with only a few tasks but can get awkward
# when the indentation gets too deep to be easily readable
time.sleep(2) # task 1
if not event.is_set()
time.sleep(1) # task 2
if not event.is_set():
time.sleep(2) # task 3
def capture():
# defining a list of functions to be executed is great when there
# are many subtasks but can be overkill if there are just a few
from functools import partial
subtasks = [
partial(time.sleep, 2), # first task
partial(time.sleep, 1), # second task
partial(time.sleep, 2) # third task
]
for subtask in subtasks:
if event.is_set():
return
subtask()
I have an infinite loop thread that sets an event when a sensor is high/true
event = threading.Event()
def eventSetter():
while True:
if sensor:
event.set()
else:
event.clear()
th1 = threading.thread(target=eventSetter)
th1.start
and I have a function capture that takes 5 sec to execute
def capture():
time.sleep(2) #sleep represents a task that takes 2 sec to finish
time.sleep(1)
time.sleep(2)
return
now I want to exit the function capture in the middle of its task whenever the event is set
for example if task 1 takes 5sec to finish and the event occurs at time 2sec, the task should not continue at time 2sec and the function should exit
I tried checking for the event every line but i don’t know how to exit in the middle of its task thus it waits for the task to finish before return applies also I didn’t like the look of multiple if/return
def capture():
time.sleep(2) #sleep represents a task that takes sec to finish
if event.is_set():
return
time.sleep(1)
if event.is_set():
return
time.sleep(2)
if event.is_set():
return
Do you want breaking loop ?
if you want break any time or condition you need to break
`key.
for example
event = threading.Event()
def eventSetter():
while True:
if sensor:
event.set()
break
#loop stoped.
else:
event.clear()
return True #or none.
You’re going to have to check the event state every chance you get in order to break out of capture
as soon as possible. The variation you provided is fine, but here are a couple other ways to do the same thing:
def capture():
# nested checks are fine with only a few tasks but can get awkward
# when the indentation gets too deep to be easily readable
time.sleep(2) # task 1
if not event.is_set()
time.sleep(1) # task 2
if not event.is_set():
time.sleep(2) # task 3
def capture():
# defining a list of functions to be executed is great when there
# are many subtasks but can be overkill if there are just a few
from functools import partial
subtasks = [
partial(time.sleep, 2), # first task
partial(time.sleep, 1), # second task
partial(time.sleep, 2) # third task
]
for subtask in subtasks:
if event.is_set():
return
subtask()