import time
import platform
fcntl = None
msvcrt = None
bLinux = True
if platform.system() != 'Windows':
fcntl = __import__("fcntl")
bLinux = True
else:
msvcrt = __import__('msvcrt')
bLinux = False
class LockTimeOutError(Exception):
pass
class UnLockTimeOutError(Exception):
pass
class FileLocker:
def __init__(self, filename=None):
if filename:
self.filename = filename + '.lock'
else:
self.filename = None
self.file = None
def lock(self, filename=None):
if filename:
self.filename = filename
self.file = open(self.filename, 'wb')
if bLinux is True:
try:
fcntl.flock(self.file, fcntl.LOCK_EX | fcntl.LOCK_NB)
except:
return False
else:
try:
msvcrt.locking(self.file.fileno(), msvcrt.LK_NBLCK, 1)
except:
return False
return True
def unlock(self, filename=None):
if filename:
self.filename = filename
self.file = open(self.filename, 'wb')
try:
if bLinux is True:
fcntl.flock(self.file, fcntl.LOCK_UN)
self.file.close()
else:
self.file.seek(0)
msvcrt.locking(self.file.fileno(), msvcrt.LK_UNLCK, 1)
return True
except:
return False
def force_lock(self, time_out=None, filename=None):
while not self.lock(filename):
time.sleep(0.1)
if time_out is not None:
time_out -= 0.1
if time_out <= 0:
raise LockTimeOutError
def force_unlock(self, time_out=None, filename=None):
while not self.unlock(filename):
time.sleep(0.1)
if time_out is not None:
time_out -= 0.1
if time_out <= 0:
raise UnLockTimeOutError