ATLAS Offline Software
Loading...
Searching...
No Matches
python.SingleAppInstance.SingleAppInstance Class Reference
Inheritance diagram for python.SingleAppInstance.SingleAppInstance:
Collaboration diagram for python.SingleAppInstance.SingleAppInstance:

Classes

class  FileLockAcquisitionError
class  FileLockReleaseError

Public Types

typedef HLT::TypeInformation::for_each_type_c< typenameEDMLIST::map, my_functor, my_result<>, my_arg< HLT::TypeInformation::get_cont, CONTAINER > >::type result

Public Member Functions

 __init__ (self, path, debug=None, blocking=False)
 acquire (self)
 release (self)
 islocked (self)
 ownlock (self)
 __del__ (self)
 __enter__ (self)
 __exit__ (self, typ, value, tb)

Public Attributes

 pid = os.getpid()
 host = socket.gethostname()
 path = path
 debug = debug
bool locked = False
 lockfile = None
 blocking = blocking

Static Public Attributes

str addr = lambda self: '%d@%s' % (self.pid, self.host)
str fddr = lambda self: '<%s %s>' % (self.path, self.addr())
str pddr

Protected Member Functions

 _readlock (self)

Detailed Description

Class to handle creating and removing lockfiles

Definition at line 13 of file SingleAppInstance.py.

Member Typedef Documentation

◆ result

Definition at line 90 of file EDM_MasterSearch.h.

Constructor & Destructor Documentation

◆ __init__()

python.SingleAppInstance.SingleAppInstance.__init__ ( self,
path,
debug = None,
blocking = False )

Definition at line 27 of file SingleAppInstance.py.

27 def __init__(self, path, debug=None, blocking = False):
28 self.pid = os.getpid()
29 self.host = socket.gethostname()
30 self.path = path
31 self.debug = debug # set this to get status messages
32 self.locked = False
33 self.lockfile = None
34 self.blocking = blocking
35

◆ __del__()

python.SingleAppInstance.SingleAppInstance.__del__ ( self)
Magic method to clean up lock when program exits

Definition at line 119 of file SingleAppInstance.py.

119 def __del__(self):
120 '''Magic method to clean up lock when program exits'''
121 self.release()
122

Member Function Documentation

◆ __enter__()

python.SingleAppInstance.SingleAppInstance.__enter__ ( self)

Definition at line 123 of file SingleAppInstance.py.

123 def __enter__(self):
124 self.acquire()
125 return self
126

◆ __exit__()

python.SingleAppInstance.SingleAppInstance.__exit__ ( self,
typ,
value,
tb )

Definition at line 127 of file SingleAppInstance.py.

127 def __exit__(self, typ, value, tb):
128 self.release()
129 return self

◆ _readlock()

python.SingleAppInstance.SingleAppInstance._readlock ( self)
protected
Internal method to read lock info

Definition at line 99 of file SingleAppInstance.py.

99 def _readlock(self):
100 '''Internal method to read lock info'''
101 try:
102 lock = {}
103 fh = open(self.path)
104 data = fh.read().rstrip().split('@')
105 fh.close()
106 lock['pid'], lock['host'] = data
107 return lock
108 except Exception:
109 return {'pid': 8**10, 'host': ''}
110
std::vector< std::string > split(const std::string &s, const std::string &t=":")
Definition hcg.cxx:177

◆ acquire()

python.SingleAppInstance.SingleAppInstance.acquire ( self)
Acquire a lock, returning self if successful, False otherwise

Definition at line 36 of file SingleAppInstance.py.

36 def acquire(self):
37 '''Acquire a lock, returning self if successful, False otherwise'''
38 if self.islocked():
39 if self.debug:
40 lock = self._readlock()
41 print('Previous lock detected: %s' % self.pddr(lock))
42 return False
43 try:
44 fd = os.open(self.path, os.O_RDWR | os.O_CREAT, 0o777)
45 fh = os.fdopen(fd, 'r+')
46 self.lockfile = fh
47 fh.seek(0)
48 try:
49 fcntlflag = fcntl.LOCK_EX
50 if not self.blocking:
51 fcntlflag |= fcntl.LOCK_NB
52 fcntl.lockf(fh, fcntlflag)
53 self.locked = True
54 except IOError:
55 print('Unable to acquire lock on %s: existing lock %s' % (self.path, fh.read()))
56 fh.close()
57 return False
58 fh.write(self.addr())
59 fh.truncate()
60 fh.flush()
61 os.fsync(fh)
62 if self.debug:
63 modtime = os.stat(self.path)
64 outstring = 'Acquired lock: '+ self.fddr() + ' at time '+ time.ctime(modtime.st_mtime)
65 print(outstring)
66 except Exception as e:
67 if os.path.isfile(self.path):
68 try:
69 # my guess is this causes problems
70 #os.unlink(self.path)
71 pass
72 except Exception:
73 pass
74 raise self.FileLockAcquisitionError(
75 'Error acquiring lock: %s, reason %s' % (self.fddr(), e))
76 return self
77
void print(char *figname, TCanvas *c1)

◆ islocked()

python.SingleAppInstance.SingleAppInstance.islocked ( self)
Check if we already have a lock

Definition at line 111 of file SingleAppInstance.py.

111 def islocked(self):
112 '''Check if we already have a lock'''
113 return self.locked
114

◆ ownlock()

python.SingleAppInstance.SingleAppInstance.ownlock ( self)
Check if we own the lock

Definition at line 115 of file SingleAppInstance.py.

115 def ownlock(self):
116 '''Check if we own the lock'''
117 return self.locked
118

◆ release()

python.SingleAppInstance.SingleAppInstance.release ( self)
Release lock, returning self

Definition at line 78 of file SingleAppInstance.py.

78 def release(self):
79 '''Release lock, returning self'''
80 if self.ownlock():
81 try:
82 fh = self.lockfile
83 fcntl.lockf(fh, fcntl.LOCK_UN)
84 self.locked = False
85 self.lockfile.close()
86 self.lockfile = None
87 if not self.blocking:
88 # some space for a small race here unfortunately
89 os.unlink(self.path)
90 if self.debug:
91 outstring = 'Released lock: ' + self.fddr() +' at time ' + time.ctime()
92 print(outstring)
93 except Exception as e:
94 print(e)
95 raise self.FileLockReleaseError(
96 'Error releasing lock: %s, reason %s' % (self.fddr(), e))
97 return self
98
static std::string release
Definition computils.h:50

Member Data Documentation

◆ addr

str python.SingleAppInstance.SingleAppInstance.addr = lambda self: '%d@%s' % (self.pid, self.host)
static

Definition at line 22 of file SingleAppInstance.py.

◆ blocking

python.SingleAppInstance.SingleAppInstance.blocking = blocking

Definition at line 34 of file SingleAppInstance.py.

◆ debug

python.SingleAppInstance.SingleAppInstance.debug = debug

Definition at line 31 of file SingleAppInstance.py.

◆ fddr

str python.SingleAppInstance.SingleAppInstance.fddr = lambda self: '<%s %s>' % (self.path, self.addr())
static

Definition at line 23 of file SingleAppInstance.py.

◆ host

python.SingleAppInstance.SingleAppInstance.host = socket.gethostname()

Definition at line 29 of file SingleAppInstance.py.

◆ locked

bool python.SingleAppInstance.SingleAppInstance.locked = False

Definition at line 32 of file SingleAppInstance.py.

◆ lockfile

python.SingleAppInstance.SingleAppInstance.lockfile = None

Definition at line 33 of file SingleAppInstance.py.

◆ path

python.SingleAppInstance.SingleAppInstance.path = path

Definition at line 30 of file SingleAppInstance.py.

◆ pddr

str python.SingleAppInstance.SingleAppInstance.pddr
static
Initial value:
= lambda self, lock: '<%s %s@%s>' %\
(self.path, lock['pid'], lock['host'])

Definition at line 24 of file SingleAppInstance.py.

◆ pid

python.SingleAppInstance.SingleAppInstance.pid = os.getpid()

Definition at line 28 of file SingleAppInstance.py.


The documentation for this class was generated from the following file: