Skip to content

Instantly share code, notes, and snippets.

@josephok
Created November 2, 2016 14:22
Show Gist options
  • Save josephok/9ccd8b9fefa31cfcda6c600beec695c3 to your computer and use it in GitHub Desktop.
Save josephok/9ccd8b9fefa31cfcda6c600beec695c3 to your computer and use it in GitHub Desktop.
python fcntl文件锁
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