Advertisement
rfmonk

threading_lock_with.py

Feb 6th, 2014
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.64 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.  
  7. import threading
  8. import logging
  9.  
  10. logging.basicConfig(level=logging.DEBUG,
  11.                     format='(%(threadName)-10s) %(message)s',
  12.                     )
  13.  
  14.  
  15. def worker_with(lock):
  16.     with lock:
  17.         logging.debug('Lock acquired via with')
  18.  
  19.  
  20. def worker_no_with(lock):
  21.     lock.acquire()
  22.     try:
  23.         logging.debug('Lock acquired directly')
  24.     finally:
  25.         lock.release()
  26.  
  27. lock = threading.Lock()
  28. w = threading.Thread(target=worker_with, args=(lock,))
  29. nw = threading.Thread(target=worker_no_with, args=(lock,))
  30.  
  31. w.start()
  32. nw.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement