The threading module

The threading module is a high-level threads module

  • Implements threads as classes (similar to Java)
  • Provides an assortment of synchronization and locking primitives.
  • Built using the low-level thread module.

Creating a new thread (as a class)

  • Idea: Inherit from the "Thread" class and provide a few methods
     import threading, time
     class PrintTime(threading.Thread):
          def __init__(self,interval):
                threading.Thread.__init__(self)    # Required
                self.interval = interval
          def run(self):
                while 1:
                     time.sleep(self.interval)
                     print time.ctime(time.time())
     
     t = PrintTime(5)    # Create a thread object
     t.start()           # Start it
     ...
<<< O'Reilly OSCON 2000, Advanced Python Programming, Slide 71
July 17, 2000, beazley@cs.uchicago.edu
>>>