Add Locking
This commit is contained in:
parent
34e68e2a9e
commit
01cb854f50
5 changed files with 48 additions and 10 deletions
25
compLib/Lock.py
Normal file
25
compLib/Lock.py
Normal file
|
@ -0,0 +1,25 @@
|
|||
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)
|
Reference in a new issue