BlogMatrix
 

GenericThread

edit David Janes 2004-10-14 14:27 UTC add comment  ·

GenericThread is a part of BlogMatrix Jäger's "generic" library providing generally useful functions. GenericThread is a wrapper around python's "threading.Thread" that provides a few useful extra functions:

  • passing of arguments when starting
  • pythoncom initialization, for applications which use Win32 COM objects
  • methods which are called before and after the main body of the thread, for initialization and teardown
  • the ability to gracefully halt thread operations

You can download GenericThread here.

Here's an example of using GenericThread:

import time
import GenericThread

class MyThread(GenericThread.GenericThread):
 def __init__(self):
  # the named parameters are optional
  GenericThread.GenericThread.__init__(self, is_daemon = False, is_com = False)
  
 def CustomizeStart(self, a, b, c):
  # the command line arguments are arbitrarly defined -- you can as many as you want
  print "CustomizeStart.CustomizeStart: called -- we're in the thread, starting up"
  
 def CustomizeFinished(self, a, b, c):
  print "CustomizeStart.CustomizeFinished: called -- we're in the thread, shutting down"
  
 def CustomizeRun(self, a, b, c):
  print "CustomizeStart.CustomizeRun: called -- we're running: do your work here"
  
  for i in xrange(10):
   self.CheckHalt()
   time.sleep(.5)
   print i, a, b, c
  
thread = MyThread()
thread.Start("a", 4784, [ 1, 2, 3 ])

Add Comment