fcntl ---fcntl and ioctl system calls — Python 3 \ .9 \ .1 documentation
lock_test.py
import os
import fcntl
import time
def process_lock():
lockfile = os.path.splitext(os.path.abspath(__file__))[0] + '.lock'
lockfp = open(lockfile, "w")
try:
fcntl.flock(lockfp, fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError:
return
return lockfp
def main():
lock = process_lock()
if not lock:
print("lock error")
exit(1)
#processing
time.sleep(360)
if __name__ == '__main__':
main()
$ python3 lock_test.py &
$ python3 lock_test.py
lock error
Recommended Posts