I was reading on this page about using a thread, catching an exception, and notifying the calling thread.
The answer suggested
Queue, but in the comments it states:
Queue is not the best vehicle to communicate an error back, unless you want to have a full queue of them. A much better construct is threading.Event()
From the Python documentation:
This is one of the simplest mechanisms for communication between threads: one thread signals an event and other threads wait for it.
I'm new to multi threading and I wanted to give it a try, so I came up with this trivial example. I'm unsure if I'm on the right track.
import threading def dividebyzero(event,first,second): try: result = first / second except ZeroDivisionError: event.set() print("Can't divide by 0") else: print(result) e = threading.Event() t1 = threading.Thread(name='DivideByZero',target=dividebyzero, args=(e, 10, 0),) t1.start() t1.join() if e.is_set(): print("Error occurred in division thread")
threading.Eventas shown in my example, is it the correct way to signal the main thread an error as occurred?
I'm not using
event.wait()because it seems unnecessary, I call
t1thread, so when I call
is_set()hopefully I'll get an accurate result. Is it mandatory to use
I understand that if I needed to raise more exceptions using an event probably isn't ideal, how would you know which exception was raise? In that situation would a
If I were to use
event.wait() there is a chance the main thread could hang forever, because if the exception doesn't occur the event is never set.
I don't have an error or problems with the code, it works, I just want it reviewed to make sure I used