33import errno
44import logging as log
55import os
6- from lockfile .pidlockfile import PIDLockFile
76
7+ try :
8+ from lockfile .pidlockfile import PIDLockFile
9+ except :
10+ pass
11+ else :
12+ class SmartPIDLockFile ( PIDLockFile ):
13+ """
14+ A PID lock file that breaks the lock if the owning process doesn't exist
15+ """
816
9- class SmartPIDLockFile ( PIDLockFile ):
10- """
11- A PID lock file that breaks the lock if the owning process doesn't exist
12- """
17+ def process_alive ( self , pid ):
18+ try :
19+ os .kill ( pid , 0 )
20+ # now we know the process exists
21+ return True
22+ except OSError as e :
23+ if e .errno == errno .ESRCH :
24+ # now we know the process doesn't exist
25+ return False
26+ else :
27+ # now we're not sure
28+ return None
1329
14- def process_alive (self , pid ):
15- try :
16- os .kill ( pid , 0 )
17- # now we know the process exists
18- return True
19- except OSError as e :
20- if e .errno == errno .ESRCH :
21- # now we know the process doesn't exist
22- return False
23- else :
24- # now we're not sure
25- return None
26-
27- def acquire (self , timeout = None ):
28- owner = self .read_pid ( )
29- if owner is not None and owner != os .getpid ( ) and self .process_alive ( owner ) is False :
30- log .warn ( "Breaking lock '%s' since owning process %i is dead."
31- % ( self .lock_file , owner ) )
32- self .break_lock ( )
33- PIDLockFile .acquire ( self , timeout )
30+ def acquire ( self , timeout = None ):
31+ owner = self .read_pid ( )
32+ if owner is not None and owner != os .getpid ( ) and self .process_alive ( owner ) is False :
33+ log .warn ( "Breaking lock '%s' since owning process %i is dead."
34+ % (self .lock_file , owner ) )
35+ self .break_lock ( )
36+ PIDLockFile .acquire ( self , timeout )
0 commit comments