The threading module (cont)

The Thread class

  • When defining threads as classes all you need to supply is the following:
    • A constructor that calls threading.Thread.__init__(self)
    • A run() method that performs the actual work of the thread.
  • A few additional methods are also available
     t.join([timeout])      # Wait for thread t to terminate
     t.getName()            # Get the name of the thread
     t.setName(name)        # Set the name of the thread
     t.isAlive()            # Return 1 if thread is alive.
     t.isDaemon()           # Return daemonic flag
     t.setDaemon(val)       # Set daemonic flag

Daemon threads

  • Normally, interpreter exits only when all threads have terminated.
  • However, a thread can be flagged as a daemon thread (runs in background).
  • Interpreter really only exits when all non-daemonic threads exit.
  • Can use this to launch threads that run forever, but which can be safely killed.
<<< O'Reilly OSCON 2000, Advanced Python Programming, Slide 72
July 17, 2000, beazley@cs.uchicago.edu
>>>