<s> from __future__ import absolute_import <EOL> import time <EOL> from contextlib import contextmanager <EOL> from gevent import sleep <EOL> from lockfile import LockFile , AlreadyLocked , LockTimeout <EOL> @ contextmanager <EOL> def lock_file ( path , maxdelay = <NUM_LIT> , lock_cls = LockFile , timeout = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> lock = lock_cls ( path ) <EOL> max_t = time . time ( ) + timeout <EOL> while True : <EOL> if time . time ( ) >= max_t : <EOL> raise LockTimeout ( "<STR_LIT>" % ( path , ) ) <EOL> try : <EOL> lock . acquire ( timeout = <NUM_LIT:0> ) <EOL> except AlreadyLocked : <EOL> sleep ( maxdelay ) <EOL> else : <EOL> try : <EOL> yield lock <EOL> break <EOL> finally : <EOL> lock . release ( ) </s>
