Skip to content

Commit d74ee0e

Browse files
committed
lockfile should silently fail if external lockfile distribution isn't installed
1 parent c844bf6 commit d74ee0e

1 file changed

Lines changed: 28 additions & 25 deletions

File tree

src/bd2k/util/lockfile.py

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,34 @@
33
import errno
44
import logging as log
55
import 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

Comments
 (0)