@contextmanager
def locked_file(fd, no_wait=False):
 locked = False
 try:
  lock = FileLock(fd)
  lock.acquire(no_wait)
  locked = True
  (yield lock)
 finally:
  if locked:
   lock.release()

@ contextmanager def locked_file ( fd , no_wait = False ) : locked = False try : lock = FileLock ( fd ) lock . acquire ( no_wait ) locked = True ( yield lock ) finally : if locked : lock . release ( )
this function is intended to be used as a with statement context manager .