Differences between revisions 1 and 2
Revision 1 as of 2002-08-12 11:57:49
Size: 908
Editor: cacher4-ext
Comment:
Revision 2 as of 2002-08-12 11:59:18
Size: 921
Editor: cacher4-ext
Comment:
Deletions are marked like this. Additions are marked like this.
Line 5: Line 5:
import time

I'd like to start this page off with a question. How do you kill one thread from within another? Here's some code that shows the problem:

import threading
import time

class Worker(threading.Thread):
  def __init__(self, eventChannel, eventHandler):
    self.eventChannel = eventChannel
    self.eventHandler = eventHandler
    self.stopFlag = 0

  def shutdown(self):
    self.stopFlag = 1

  def run(self):
    self.stopFlag = 0
    while(!self.stopFlag):
      event = self.eventChannel.waitEvent()
      self.eventHandler.dispatchEvent(event)

eventChannel = EventChannel()
eventHandler = eventHandler()

worker = Worker(eventChannel, eventHandler)
worker.run()
time.sleep(20)
worker.shutdown()

The problem here is that EventChannel.waitEvent() is a blocking operation. So if no event ever arrives, then our worker thread never stops. Suggestions? --ChrisSteinbach

ThreadProgramming (last edited 2008-11-15 13:59:41 by localhost)

Unable to edit the page? See the FrontPage for instructions.