盒子
盒子

Python 实践:文件锁

import fcntl
class FileLock(object):
def __init__(self, file_name):
self.file_name = file_name
self.handle = open(file_name, 'a+')

def lock(self):
try:
fcntl.flock(self.handle, fcntl.LOCK_EX | fcntl.LOCK_NB)
return True
except Exception as e:
print(e)
return False

def unlock(self):
fcntl.flock(self.handle, fcntl.LOCK_UN)


def __del__(self):
try:
self.handle.close()
except Exception as e:
print(e)
pass
支持一下
扫一扫,支持forsigner
  • 微信扫一扫
  • 支付宝扫一扫