from filelock import FileLock, Timeout import atexit FILELOCK_PATH = "/root/complib.lock" global_lock = FileLock(FILELOCK_PATH, timeout=1) class Lock(object): @staticmethod def lock(): global_lock.acquire() @staticmethod def unlock(): global_lock.release() @staticmethod def is_locked(): try: global_lock.acquire() global_lock.release() return False except Timeout: return True atexit.register(Lock.unlock)