[Tutor] stopping threads?

Marcus Goldfish magoldfish at gmail.com
Thu Sep 29 02:42:27 CEST 2005


I'm a little confused about threading in Python. Here is a sample class I've
written for monitoring for events. Included in the comments are a couple of
questions, specifically:
  (1) can I use super() in the constructor, or is direct call to base class
preferred?
 (2) do I need to sleep in the run() method? If so, why? It seems to improve
my programs responsiveness
 (3) what happens after run() finishes-- does the thread die, get suspended,
go away? Should I try to force the thread into one of these states, and if
so how?
 Any help is appreciated!
Thanks,
Marcus
  class Monitor(threading.Thread):
 def __init__(self):
 threading.Thread.__init__(self) # would super() also work here? which is
preferred
 self.undetected = True # presumably, when the event occurs it sets this
attribute to False
  def run(self):
 print "looking for event"
 while self.undetected is True:
 time.sleep(0.1) # another minor point: why do I need to sleep here?
 self.processEvent()
 # what happens here-- does the thread die?
  def processEvent(self):
 print "yeah-- event detected"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050928/033aefd5/attachment.html


More information about the Tutor mailing list