boolean flag vs threading.Event

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel

    #1

    boolean flag vs threading.Event

    I have a class similar to this:


    class MyThread(thread ing.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.Threa d.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(thread ing.Thread):

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

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

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

  • Chris Mellon

    #2
    Re: boolean flag vs threading.Event

    On 27 Feb 2007 13:37:12 -0800, Daniel <millerdev@gmai l.comwrote:
    I have a class similar to this:
    >
    >
    class MyThread(thread ing.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.Threa d.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(thread ing.Thread):
    >
    def __init__(self):
    self.event = threading.Event ()
    >
    def run(self):
    while not self.event.isSe t():
    pass # do stuff here
    >
    def join(self):
    self.event.set( )
    threading.Threa d.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?

    Comment

    • Daniel

      #3
      Re: boolean flag vs threading.Event

      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

      Comment

      Working...