Locks and Blocking

By default, all locking primitives block until lock is acquired

  • In general, this is uninterruptible.

Fortunately, most primitives provide a non-blocking option

     
     if not lck.acquire(0):
         # lock couldn't be acquired! 
  • This works for Lock, RLock, and Semaphore objects

Timeouts

  • Condition variables and events provide a timeout option
     cv = Condition()
     ...
     cv.wait(60.0)    # Wait 60 seconds for notification
  • On timeout, the function simply returns. Up to caller to detect errors.
<<< O'Reilly OSCON 2000, Advanced Python Programming, Slide 79
July 17, 2000, beazley@cs.uchicago.edu
>>>