-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingleton.py
88 lines (78 loc) · 2.75 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# -*- coding: utf-8 -*-
import sys
import os
import errno
import tempfile
import unittest
import logging
from multiprocessing import Process
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class SingleInstance:
def __init__(self, flavor_id=""):
import sys
self.initialized = False
basename = os.path.splitext(os.path.abspath(sys.argv[0]))[0].replace(
"/", "-").replace(":", "").replace("\\", "-") + '-%s' % flavor_id + '.lock'
self.lockfile = os.path.normpath(
tempfile.gettempdir() + '/' + basename)
logger.debug("SingleInstance lockfile: " + self.lockfile)
if sys.platform == 'win32':
try:
if os.path.exists(self.lockfile):
os.unlink(self.lockfile)
self.fd = os.open(
self.lockfile, os.O_CREAT | os.O_EXCL | os.O_RDWR)
except OSError:
type, e, tb = sys.exc_info()
if e.errno == 13:
logger.error(
"Another instance is already running, quitting.")
QMessageBox.information(None, u"信息提示", u"程序已经在运行!")
sys.exit(-1)
print(e.errno)
raise
else: # non Windows
import fcntl
self.fp = open(self.lockfile, 'w')
try:
fcntl.lockf(self.fp, fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError:
logger.warning(
"Another instance is already running, quitting.")
QMessageBox.information(None, u"信息提示", u"程序已经在运行!")
sys.exit(-1)
self.initialized = True
def __del__(self):
import sys
import os
if not self.initialized:
return
try:
if sys.platform == 'win32':
if hasattr(self, 'fd'):
os.close(self.fd)
os.unlink(self.lockfile)
else:
import fcntl
fcntl.lockf(self.fp, fcntl.LOCK_UN)
# os.close(self.fp)
if os.path.isfile(self.lockfile):
os.unlink(self.lockfile)
except Exception as e:
if logger:
logger.warning(e)
else:
print("Unloggable error: %s" % e)
sys.exit(-1)
def f(name):
tmp = logger.level
logger.setLevel(logging.CRITICAL) # we do not want to see the warning
me2 = SingleInstance(flavor_id=name)
logger.setLevel(tmp)
pass
logger = logging.getLogger("tendo.singleton")
logger.addHandler(logging.StreamHandler())
if __name__ == "__main__":
logger.setLevel(logging.DEBUG)
unittest.main()