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

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to