Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # this is from The Python
- # Standard Library by example
- # ISBN13: 9780321767349
- import logging
- import threading
- import time
- logging.basicConfig(level=logging.DEBUG,
- format='(%(threadName)-10s) %(message)s',
- )
- def lock_holder(lock):
- logging.debug('Starting')
- while True:
- lock.aquire()
- try:
- logging.debug('Holding')
- time.sleep(0.5)
- finally:
- logging.debug('Not holding')
- lock.release()
- time.sleep(0.5)
- return
- def worker(lock):
- logging.debug('Starting')
- num_tries = 0
- num_acquires = 0
- while num_acquires < 3:
- time.sleep(0.5)
- logging.debug('Trying to acquire')
- have_it = lock.acquire(0)
- try:
- num_tries += 1
- if have_it:
- logging.debug('Iteration %d: Acquired', num_tries)
- num_acquires += 1
- else:
- logging.debug('Iteration %d: Not acquired', num_tries)
- finally:
- if have_it:
- lock.release()
- logging.debug('Done after %d iterations', num_tries)
- lock = threading.lock()
- holder = threading.Thread(target=lock_holder,
- args=(lock,),
- name='LockHolder')
- holder.setDaemon(True)
- holder.start()
- worker = threading.Thread(target=worker,
- args=(lock,),
- name='Worker')
- worker.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement