我正在尝试使用zc.lockfile。我看到在与我的 python 脚本相同的目录中创建了一个锁定文件,但是当我按 ctrl+C 时,该文件没有被删除。我注册了一个回调,甚至已经测试了很长时间(不确定 zc.lockfile 是否会产生一个新线程并且需要时间来完成)。
import os
import sys
import signal
import time
import zc.lockfile
program_lock = None
def onExitCodePressed(signal, frame):
"""Callback run on a premature user exit."""
global program_lock
print '\r\nYou pressed Ctrl+C'
program_lock.close()
time.sleep(5)
sys.exit(0)
def main():
signal.signal(signal.SIGINT, onExitCodePressed)
if os.path.exists('myapp_lock'):
print "\nAnother instance of the program is already running.\n"
sys.exit(0)
else:
program_lock = zc.lockfile.LockFile('myapp_lock')
while True:
continue
if __name__ == '__main__':
main()