ATLAS Offline Software
Classes | Public Member Functions | Public Attributes | Static Public Attributes | Private Member Functions | List of all members
python.SingleAppInstance.SingleAppInstance Class Reference
Inheritance diagram for python.SingleAppInstance.SingleAppInstance:
Collaboration diagram for python.SingleAppInstance.SingleAppInstance:

Classes

class  FileLockAcquisitionError
 
class  FileLockReleaseError
 

Public Member Functions

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

Public Attributes

 pid
 
 host
 
 path
 
 debug
 
 locked
 
 lockfile
 
 blocking
 

Static Public Attributes

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

Private Member Functions

def _readlock (self)
 

Detailed Description

Class to handle creating and removing lockfiles

Definition at line 13 of file SingleAppInstance.py.

Constructor & Destructor Documentation

◆ __init__()

def 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__()

def 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__()

def 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__()

def 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()

def python.SingleAppInstance.SingleAppInstance._readlock (   self)
private
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 

◆ acquire()

def 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 

◆ islocked()

def 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()

def 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()

def 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 

Member Data Documentation

◆ addr

string 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

Definition at line 34 of file SingleAppInstance.py.

◆ debug

python.SingleAppInstance.SingleAppInstance.debug

Definition at line 31 of file SingleAppInstance.py.

◆ fddr

string 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

Definition at line 29 of file SingleAppInstance.py.

◆ locked

python.SingleAppInstance.SingleAppInstance.locked

Definition at line 32 of file SingleAppInstance.py.

◆ lockfile

python.SingleAppInstance.SingleAppInstance.lockfile

Definition at line 33 of file SingleAppInstance.py.

◆ path

python.SingleAppInstance.SingleAppInstance.path

Definition at line 30 of file SingleAppInstance.py.

◆ pddr

string python.SingleAppInstance.SingleAppInstance.pddr = lambda self, lock: '<%s %s@%s>' %\
static

Definition at line 24 of file SingleAppInstance.py.

◆ pid

python.SingleAppInstance.SingleAppInstance.pid

Definition at line 28 of file SingleAppInstance.py.


The documentation for this class was generated from the following file:
print
void print(char *figname, TCanvas *c1)
Definition: TRTCalib_StrawStatusPlots.cxx:25
python.EventInfoMgtInit.release
release
Definition: EventInfoMgtInit.py:23
python.processes.powheg.ZZ.ZZ.__init__
def __init__(self, base_directory, **kwargs)
Constructor: all process options are set here.
Definition: ZZ.py:18
Trk::open
@ open
Definition: BinningType.h:40
python.AthDsoLogger.__del__
def __del__(self)
Definition: AthDsoLogger.py:81
Trk::split
@ split
Definition: LayerMaterialProperties.h:38