文件锁可以构建自己的分布式队列 现在只能实现在低速情况下 依赖文件的读取文件锁实现主要依赖一个大神开源的 FileLock类 非常强悍
import os
import time
import errno
class FileLockException(Exception):
pass
class FileLock(object):
""" A file locking mechanism that has context-manager support so
you can use it in a with statement. This should be relatively cross
compatible as it doesn't rely on msvcrt or fcntl for the locking.
"""
def __init__(self, file_name, timeout=10, delay=.05):
""" Prepare the file locker. Specify the file to lock and optionally
the maximum timeout and the delay between each attempt to lock.
"""
self.is_locked = False
self.lockfile = os.path.join(os.getcwd(), "%s.lock" % file_name)
self.file_name = file_name
self.timeout = timeout
self.delay = delay
def acquire(self):
""" Acquire the lock, if possible. If the lock is in use, it check again
every `wait` seconds. It does this until it either gets the lock or
exceeds `timeout` number of seconds, in which case it throws
an exception.
"""
start_time = time.time()
while True:
try:
#独占式打开文件
self.fd = os.open(self.lockfile, os.O_CREAT|os.O_EXCL|os.O_RDWR)
break;
except OSError as e:
if e.errno != errno.EEXIST:
raise
if (time.time() - start_time) >= self.timeout:
raise FileLockException("Timeout occured.")
time.sleep(self.delay)
self.is_locked = True
def release(self):
""" Get rid of the lock by deleting the lockfile.
When working in a `with` statement, this gets automatically
called at the end.
"""
#关闭文件,删除文件
if self.is_locked:
os.close(self.fd)
os.unlink(self.lockfile)
self.is_locked = False
def __enter__(self):
""" Activated when used in the with statement.
Should automatically acquire a lock to be used in the with block.
"""
if not self.is_locked:
self.acquire()
return self
def __exit__(self, type, value, traceback):
""" Activated at the end of the with statement.
It automatically releases the lock if it isn't locked.
"""
if self.is_locked:
self.release()
def __del__(self):
""" Make sure that the FileLock instance doesn't leave a lockfile
lying around.
"""
self.release()
"""
#use as:
from filelock import FileLock
with FileLock("myfile.txt"):
# work with the file as it is now locked
print("Lock acquired.")
"""
在我的应用场景下需要多个程序去读一个文件的数据执行 且不能重复执行 虽然速度较慢 也算是一个分布式应用
实现原理是借助一个csv文件将执行index写入文件 每次执行读取index执行 读取写入过程中对文件加锁
import csv
import time
from filelock import FileLock
def filelock(filename):
with FileLock(filename):#加锁只需要这一步
with open(filename,mode='r+',encoding='utf-8-sig') as f:
rowlist= []
for row in csv.reader(f):
rowlist.append(row)
#rowlist读取文件中的index
if rowlist !=[]:
start_flags= int(rowlist[0][0])
else:
start_flags = 0
f.seek(0)
#加一之后再写入文件
f.write(str(start_flags+1))
#完成之后等待解锁
time.sleep(2)
return start_flags
目前执行过程中发现需要注意的问题 完成之后一定要做等待 文件的关闭需要一些时间
如果一个程序在写入过程中出问题 会导致后续程序都卡死 待优化
通过这个功能的初步完成 也对文件的操作权限有了全面一点的了解
r+可读内容可写内容调整指针可覆盖 w+先清空文件在覆盖写可读写入后的内容 a+追加写入不可读
f.seek(0)#将指针调整到开头 在写入就覆盖