1ba991379Sopenharmony_ci#encoding=utf-8 2ba991379Sopenharmony_ciimport os 3ba991379Sopenharmony_ciimport time 4ba991379Sopenharmony_ciimport errno 5ba991379Sopenharmony_ci 6ba991379Sopenharmony_ciclass FileLockException(Exception): 7ba991379Sopenharmony_ci pass 8ba991379Sopenharmony_ci 9ba991379Sopenharmony_ciclass FileLock(object): 10ba991379Sopenharmony_ci """ A file locking mechanism that has context-manager support so 11ba991379Sopenharmony_ci you can use it in a with statement. This should be relatively cross 12ba991379Sopenharmony_ci compatible as it doesn't rely on msvcrt or fcntl for the locking. 13ba991379Sopenharmony_ci """ 14ba991379Sopenharmony_ci 15ba991379Sopenharmony_ci 16ba991379Sopenharmony_ci def __init__(self, timeout=7200, delay=30): 17ba991379Sopenharmony_ci """ Prepare the file locker. Specify the file to lock and optionally 18ba991379Sopenharmony_ci the maximum timeout and the delay between each attempt to lock. 19ba991379Sopenharmony_ci """ 20ba991379Sopenharmony_ci self.is_locked = False 21ba991379Sopenharmony_ci self.timeout = timeout 22ba991379Sopenharmony_ci self.delay = delay 23ba991379Sopenharmony_ci 24ba991379Sopenharmony_ci def _setLockFileName(self, file_name): 25ba991379Sopenharmony_ci self.lockfile = file_name 26ba991379Sopenharmony_ci 27ba991379Sopenharmony_ci def acquire(self): 28ba991379Sopenharmony_ci """ Acquire the lock, if possible. If the lock is in use, it check again 29ba991379Sopenharmony_ci every `wait` seconds. It does this until it either gets the lock or 30ba991379Sopenharmony_ci exceeds `timeout` number of seconds, in which case it throws 31ba991379Sopenharmony_ci an exception. 32ba991379Sopenharmony_ci """ 33ba991379Sopenharmony_ci start_time = time.time() 34ba991379Sopenharmony_ci if os.path.isfile(self.lockfile): 35ba991379Sopenharmony_ci try: 36ba991379Sopenharmony_ci mark_file_mtime = os.path.getmtime(self.lockfile) 37ba991379Sopenharmony_ci if (start_time - mark_file_mtime > self.timeout): 38ba991379Sopenharmony_ci os.remove(self.lockfile) 39ba991379Sopenharmony_ci except Exception as e: 40ba991379Sopenharmony_ci print("the lock file is locked by other process") 41ba991379Sopenharmony_ci 42ba991379Sopenharmony_ci while True: 43ba991379Sopenharmony_ci try: 44ba991379Sopenharmony_ci #open file , other application can't open it 45ba991379Sopenharmony_ci self.fd = os.open(self.lockfile, os.O_CREAT|os.O_EXCL|os.O_RDWR) 46ba991379Sopenharmony_ci break 47ba991379Sopenharmony_ci except OSError as e: 48ba991379Sopenharmony_ci if e.errno != errno.EEXIST: 49ba991379Sopenharmony_ci raise 50ba991379Sopenharmony_ci if (time.time() - start_time) >= self.timeout: 51ba991379Sopenharmony_ci raise FileLockException("Timeout occured.") 52ba991379Sopenharmony_ci time.sleep(self.delay) 53ba991379Sopenharmony_ci self.is_locked = True 54ba991379Sopenharmony_ci 55ba991379Sopenharmony_ci 56ba991379Sopenharmony_ci def release(self): 57ba991379Sopenharmony_ci """ Get rid of the lock by deleting the lockfile. 58ba991379Sopenharmony_ci When working in a `with` statement, this gets automatically 59ba991379Sopenharmony_ci called at the end. 60ba991379Sopenharmony_ci """ 61ba991379Sopenharmony_ci # 62ba991379Sopenharmony_ci if self.is_locked : 63ba991379Sopenharmony_ci os.close(self.fd) 64ba991379Sopenharmony_ci os.unlink(self.lockfile) 65ba991379Sopenharmony_ci self.is_locked = False 66ba991379Sopenharmony_ci 67ba991379Sopenharmony_ci def lockFile(self, file_name): 68ba991379Sopenharmony_ci """ Activated when used in the with statement. 69ba991379Sopenharmony_ci Should automatically acquire a lock to be used in the with block. 70ba991379Sopenharmony_ci """ 71ba991379Sopenharmony_ci self._setLockFileName(file_name) 72ba991379Sopenharmony_ci if not self.is_locked: 73ba991379Sopenharmony_ci self.acquire() 74ba991379Sopenharmony_ci return self 75ba991379Sopenharmony_ci 76ba991379Sopenharmony_ci def releaseFile(self): 77ba991379Sopenharmony_ci """ Activated at the end of the with statement. 78ba991379Sopenharmony_ci It automatically releases the lock if it isn't locked. 79ba991379Sopenharmony_ci """ 80ba991379Sopenharmony_ci if self.is_locked: 81ba991379Sopenharmony_ci self.release() 82ba991379Sopenharmony_ci 83ba991379Sopenharmony_ci def __del__(self): 84ba991379Sopenharmony_ci """ Make sure that the FileLock instance doesn't leave a lockfile 85ba991379Sopenharmony_ci lying around. 86ba991379Sopenharmony_ci """ 87ba991379Sopenharmony_ci self.release() 88ba991379Sopenharmony_ci 89ba991379Sopenharmony_ci 90ba991379Sopenharmony_ci 91ba991379Sopenharmony_ci#用法比较有意思,使用with关键字。对with关键字来说,FileLock类先执行__enter__函数,然后,执行with块里的那些代码,执行完了之后,再执行__exit__函数,等价于相当于如下形式: 92ba991379Sopenharmony_ci#try: 93ba991379Sopenharmony_ci# 执行 __enter__的内容 94ba991379Sopenharmony_ci# 执行 with_block. 95ba991379Sopenharmony_ci#finally: 96ba991379Sopenharmony_ci# 执行 __exit__内容 97ba991379Sopenharmony_ci#FileLock在__enter__函数独占式创建或打开一个文件,这个文件不会被其他程序或者进程再次创建或者打开,由此形成lock,执行完代码,在__exit__里,关闭并删除文件