Last active
March 3, 2023 13:56
-
-
Save jossef/8979d859de9dc163aa5ecafa6216859d to your computer and use it in GitHub Desktop.
python run once
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 sys | |
import fcntl | |
import os | |
_run_once_file_handle = 0 | |
def run_once(): | |
global _run_once_file_handle | |
host_script_file_path = os.path.realpath(sys.argv[0]) | |
_run_once_file_handle = open(host_script_file_path, 'r') | |
try: | |
fcntl.flock(_run_once_file_handle, fcntl.LOCK_EX | fcntl.LOCK_NB) | |
except: | |
sys.exit('Another instance already running') |
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 common | |
import time | |
common.run_once() | |
print ('sleeping') | |
time.sleep(10) | |
print ('done') |
The same behaviour experienced od Debian 11 as dachshund-digital described for Deb9.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This does not appear to work on Debian 9 stretch. Both instances of script run and both acquire lock. I am executing the scripts from different SSH sessions.