【发布时间】:2016-12-26 16:21:35
【问题描述】:
我正在尝试为在集群上运行的软件实现基于文件系统的锁定。底层共享文件系统使用DRBD 实现,因此可以认为保证了同步性。我当前的实现如下所示:
# check the lock file
if os.path.isfile(lockfile):
if time.time() - os.path.getmtime(lockfile) > 3600:
logInfo('lock is older than 3600s, removing it and going on collecting result.')
os.remove(lockfile)
else:
logInfo('Previous action is still on-going, please wait until it\'s finished or timeout.')
sys.exit(1)
# create the lock file
open(lockfile, 'w').close();
显然,当集群中不同机器上运行的多个脚本实例可能会一致认为系统已解锁,创建锁定文件并执行需要互斥的操作时,可能会出现这种情况。
总而言之,我需要一个基于文件系统的锁定工具,并且锁定检查和创建一起形成一个原子操作。
使用lockfile 命令可以在shell 脚本中实现相同的功能。
【问题讨论】: