I am trying to add a timeout function,
def doit(stop_event, arg):
while not stop_event.is_set(): #currently, to stop the thread, Event needs to be checked in the while condition
<calculation part> # if the calculation part takes more time, then the thread needs to be stopped
print ("working on %s" % arg)
print("Stopping as you wish.")
def main():
pill2kill = threading.Event()
t = threading.Thread(target=doit, args=(pill2kill, "task"))
t.start()
time.sleep(5)
pill2kill.set()
t.join()
In this code, the timeout works using the Event, but it needs to be checked whether, the event is set or not in while loop.
But, How to stop the thread - if the <calculation part> takes more time[more than 5 seconds], then at that time also the thread needs to be stopped [the thread should not wait until the completion of <calculation part> and then check for the Event to decide to stop the thread]
is there is a way to do that using Thread in python?, I go through most of the blogs, all of the methods are using the loop concept to check to stop/continue the thread, is there any way to do without for/while loop?
Thanks