Created
November 2, 2016 14:22
-
-
Save josephok/9ccd8b9fefa31cfcda6c600beec695c3 to your computer and use it in GitHub Desktop.
python fcntl文件锁
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fcntl | |
import time | |
pidfile = "1.pid" | |
with open(pidfile, "w") as f: | |
try: | |
fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB) | |
while True: | |
print("hello") | |
time.sleep(5) | |
except BlockingIOError: | |
print("already running, quit!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment