469,641 Members | 1,174 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,641 developers. It's quick & easy.

boolean flag vs threading.Event

I have a class similar to this:
class MyThread(threading.Thread):

def __init__(self):
self.terminated = False

def run(self):
while not self.terminated:
pass # do stuff here

def join(self):
self.terminated = True
threading.Thread.join(self)
Recently I was reading in the Python Cookbook (9.2 Terminating a
Thread) about how to do this sort of thing. That recipe uses a
threading.Event object to signal the thread termination. Here's my
class recoded to use an event:
class MyThread(threading.Thread):

def __init__(self):
self.event = threading.Event()

def run(self):
while not self.event.isSet():
pass # do stuff here

def join(self):
self.event.set()
threading.Thread.join(self)
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?

Thanks,
~ Daniel

Feb 27 '07 #1
2 4261
On 27 Feb 2007 13:37:12 -0800, Daniel <mi*******@gmail.comwrote:
I have a class similar to this:
class MyThread(threading.Thread):

def __init__(self):
self.terminated = False

def run(self):
while not self.terminated:
pass # do stuff here

def join(self):
self.terminated = True
threading.Thread.join(self)
Recently I was reading in the Python Cookbook (9.2 Terminating a
Thread) about how to do this sort of thing. That recipe uses a
threading.Event object to signal the thread termination. Here's my
class recoded to use an event:
class MyThread(threading.Thread):

def __init__(self):
self.event = threading.Event()

def run(self):
while not self.event.isSet():
pass # do stuff here

def join(self):
self.event.set()
threading.Thread.join(self)
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.

There's a whole bunch of other cases where you might want to use an
event, too. For example, you can have a single event which signals
multiple threads to stop, and you can wait on a thread without busy
looping.

If you don't care about doing any of those things, and you're
confident in relying on undocumented features of the GIL to protect
you, and you'll never port your code to a different Python
implementation, then I guess you can go ahead and use the boolean. But
what are you gaining, really?
Feb 27 '07 #2
But what are you gaining, really [by using a boolean flag instead of an Event]?

I agree Chris, the Event is better and it certainly does not add much
if any overhead. Thanks for the response.

~ Daniel

Feb 28 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by Ian Tuomi | last post: by
76 posts views Thread by KimmoA | last post: by
4 posts views Thread by moondaddy | last post: by
11 posts views Thread by KiranJyothi | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.