Advertisement
rfmonk

threading_enumerate.py

Feb 4th, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.91 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # this is from The Python
  4. # Standard Library by example
  5. # ISBN13: 9780321767349
  6. # This program didn't work on my system
  7. # and illuminated a bug in logging.
  8. import random
  9. import threading
  10. import time
  11. import logging
  12.  
  13. logging.basicConfig(level=logging.DEBUG,
  14.                     format='(%(threadingName)-10s) %(message)s',
  15.                     )
  16.  
  17.  
  18. def worker():
  19.     """thread worker function"""
  20.     t = threading.currentThread()  # pyflakes complains here. local
  21.     # variable 't' is assigned but never used.
  22.     pause = random.randint(1, 5)
  23.     logging.debug('sleeping %s', pause)
  24.     time.sleep(pause)
  25.     logging.debug('ending')
  26.     return
  27.  
  28. for i in range(3):
  29.     t = threading.Thread(target=worker)
  30.     t.setDaemon(True)
  31.     t.start()
  32.  
  33. main_thread = threading.currentThread()
  34. for t in threading.enumerate():
  35.     if t is main_thread:
  36.         continue
  37.     logging.debug('joining %s', t.getName())
  38.     t.join()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement