Created
November 8, 2014 04:14
-
-
Save mckelvin/0b799d940409d94208fe to your computer and use it in GitHub Desktop.
This file contains 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 os | |
import sys | |
import fcntl | |
import errno | |
def lock(fd): | |
try: | |
fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) | |
except IOError, e: | |
if e.errno in (errno.EACCES, errno.EAGAIN): | |
sys.exit(0) | |
else: | |
raise | |
def single_process(fn): | |
lock_file_name = 'the_same_lock_name.lock' | |
lock_file_path = os.path.join('/tmp/', lock_file_name) | |
def _(*a, **kw): | |
fd = os.open(lock_file_path, os.O_CREAT | os.O_RDWR, 0660) | |
try: | |
lock(fd) | |
return fn(*a, **kw) | |
finally: | |
os.close(fd) | |
return _ |
This file contains 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 time | |
from common import single_process | |
@single_process | |
def main(): | |
print 'begin script1' | |
time.sleep(1) | |
print 'end script1' | |
if __name__ == '__main__': | |
main() |
This file contains 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 time | |
from common import single_process | |
@single_process | |
def main(): | |
print 'begin script2' | |
time.sleep(1) | |
print 'end script2' | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment