Created
February 10, 2013 15:31
-
-
Save nitely/4749943 to your computer and use it in GitHub Desktop.
Python application cross-platform single instance.
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 sys | |
import os | |
try: | |
import fcntl | |
except ImportError: | |
fcntl = None | |
LOCK_PATH = os.path.join(os.path.abspath(os.path.dirname(sys.argv[0])), "lock") | |
OS_WIN = False | |
if 'win32' in sys.platform.lower(): | |
OS_WIN = True | |
class SingleInstance: | |
def __init__(self): | |
self.fh = None | |
self.is_running = False | |
self.do_magic() | |
def do_magic(self): | |
if OS_WIN: | |
try: | |
if os.path.exists(LOCK_PATH): | |
os.unlink(LOCK_PATH) | |
self.fh = os.open(LOCK_PATH, os.O_CREAT | os.O_EXCL | os.O_RDWR) | |
except EnvironmentError as err: | |
if err.errno == 13: | |
self.is_running = True | |
else: | |
raise | |
else: | |
try: | |
self.fh = open(LOCK_PATH, 'w') | |
fcntl.lockf(self.fh, fcntl.LOCK_EX | fcntl.LOCK_NB) | |
except EnvironmentError as err: | |
if self.fh: | |
self.is_running = True | |
else: | |
raise | |
def clean_up(self): | |
# this is not really needed | |
try: | |
if OS_WIN and self.fh: | |
self.fh.close() | |
os.unlink(LOCK_PATH) | |
elif self.fh: | |
fcntl.lockf(self.fh, fcntl.LOCK_UN) | |
self.fh.close() # ??? | |
os.unlink(LOCK_PATH) | |
except Exception as err: | |
# logger.exception(err) | |
pass | |
if __name__ == "__main__": | |
import time | |
si = SingleInstance() | |
try: | |
if si.is_running: | |
sys.exit("This app is already running!") | |
time.sleep(20) | |
# do other stuff | |
finally: | |
si.clean_up() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tested on Python 2.7. The sleep is not needed, I included it for further testing.