I saw examples e.g. here of using an Event
to stop a thread where I think a boolean flag would do the job.
Event
class MyThread(threading.Thread):
def __init__(self):
self._please_stop = threading.Event()
def run(self):
while not self._please_stop.is_set():
[...]
def stop(self):
self._please_stop.set()
Flag
class MyThread(threading.Thread):
def __init__(self):
self._please_stop = False
def run(self):
while not self._please_stop:
[...]
def stop(self):
self._please_stop = True
What is the benefit of using an Event
, here? Its wait
method is not used. What makes it better than a boolean flag?
I can see the point if the same Event
is shared among several threads, but otherwise, I don't get it.
This mailing list thread suggests that Event
would be safer, but it's unclear to me why.
More precisely, I don't understand those two paragraphs:
If I understand the GIL correctly, it synchronizes all access to Python data structures (such as my boolean 'terminated' flag). If that is the case, why bother using threading.Event for this purpose?
The GIL is an implementation detail and relying on it to synchronize things for you isn't futureproof. You're likely to have lots of warning, but using threading.Event() isn't any harder, and it's more correct and safer in the long term.
I agree that using an Event
adds close to no overhead, so I can stick to that, but I'd like to understand the limits of the flag approach.
(I'm using Python3, so I'm not concerned by Python2 limitations, if any, although those would be totally worth mentioning here.)