@contextmanager
def lock_path(directory, timeout=10):
 mkdirs(directory)
 lockpath = ('%s/.lock' % directory)
 fd = os.open(lockpath, (os.O_WRONLY | os.O_CREAT))
 try:
  with LockTimeout(timeout, lockpath):
   while True:
    try:
     fcntl.flock(fd, (fcntl.LOCK_EX | fcntl.LOCK_NB))
     break
    except IOError as err:
     if (err.errno != errno.EAGAIN):
      raise
    sleep(0.01)
  (yield True)
 finally:
  os.close(fd)

@ contextmanager def lock_path ( directory , timeout = 10 ) : mkdirs ( directory ) lockpath = ( '%s/.lock' % directory ) fd = os . open ( lockpath , ( os . O_WRONLY | os . O_CREAT ) ) try : with LockTimeout ( timeout , lockpath ) : while True : try : fcntl . flock ( fd , ( fcntl . LOCK_EX | fcntl . LOCK_NB ) ) break except IOError as err : if ( err . errno != errno . EAGAIN ) : raise sleep ( 0.01 ) ( yield True ) finally : os . close ( fd )
context manager that acquires a lock on a directory .