25
25
#class used to handle one application instance mechanism
26
class ApplicationInstance:
28
#specify the file used to save the application instance pid
29
def __init__( self, pid_file ):
30
self.pid_file = pid_file
32
self.startApplication()
34
#check if the current application is already running
36
#check if the pidfile exists
37
if not os.path.isfile( self.pid_file ):
40
#read the pid from the file
43
file = open( self.pid_file, 'rt' )
50
#check if the process with specified by pid exists
55
os.kill( pid, 0 ) #this will raise an exception if the pid is not valid
60
print "The application is already running !"
61
exit(0) #exit raise an exception so don't put it in a try/except block
63
#called when the single instance starts to save it's pid
64
def startApplication( self ):
65
file = open( self.pid_file, 'wt' )
66
file.write( str( os.getpid() ) )
69
#called when the single instance exit ( remove pid file )
70
def exitApplication( self ):
72
os.remove( self.pid_file )
26
class ApplicationInstance(object):
28
#specify the file used to save the application instance pid
29
def __init__(self, pid_file):
30
self.pid_file = pid_file
32
#check if the current application is already running
34
#check if the pidfile exists
35
if not os.path.isfile( self.pid_file ):
38
#read the pid from the file
41
file = open(self.pid_file, 'rt')
49
#check if the process with specified by pid exists
53
#this will raise an exception if the pid is not valid
64
#called when the single instance starts to save it's pid
65
def startApplication(self):
66
file = open(self.pid_file, 'wt')
67
file.write(str(os.getpid()))
70
#called when the single instance exit ( remove pid file )
71
def exitApplication(self):
73
os.remove(self.pid_file)