Python Threading with Event object -
i've seen lot of python scripts use threads in class , lot of them use threading.event()
. example:
class timerclass(threading.thread): def __init__(self): threading.thread.__init__(self) self.event = threading.event() def run(self): while not self.event.is_set(): print "something" self.event.wait(120)
in while
loop, why check condition if don't set self.event
?
because else set it.
you start thread in 1 part of application , continue whatever do:
thread = timerclass() thread.start() # stuff
the thread it's stuff, while stuff. if want terminate thread call:
thread.event.set()
and thread stop.
so answer is: event, in case, not used controlling thread inside thread object itself. used controlling thread outside (from object holds reference thread).
Comments
Post a Comment