-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsingleton.py
39 lines (35 loc) · 1.24 KB
/
singleton.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import logging
import os, sys
logger = logging.getLogger("singleton")
# code adapted from https://github.com/pycontribs/tendo/blob/master/tendo/singleton.py
class InstanceFileLock:
def __init__(self, lockpath):
self._lock = lockpath
def __enter__(self):
if sys.platform == 'win32':
try:
if os.path.exists(self._lock):
os.unlink(self._lock)
self.fd = os.open(self._lock, os.O_CREAT | os.O_EXCL | os.O_RDWR)
except OSError:
logger.warning("Another instance is running.")
sys.exit(-1)
else:
import fcntl
self.fp = open(self._lock, 'w')
try:
fcntl.lockf(self.fp, fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError:
logger.warning("Another instance is running.")
sys.exit(-1)
def __exit__(self, type, value, traceback):
try:
if sys.platform == 'win32':
os.close(self.fd)
else:
import fcntl
fcntl.lockf(self.fp, fcntl.LOCK_UN)
self.fp.close()
os.unlink(self._lock)
except Exception as e:
logger.error(e)