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 14 of file SingleAppInstance.py.

Constructor & Destructor Documentation

◆ __init__()

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

Definition at line 28 of file SingleAppInstance.py.

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

◆ __del__()

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

Definition at line 120 of file SingleAppInstance.py.

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

Member Function Documentation

◆ __enter__()

def python.SingleAppInstance.SingleAppInstance.__enter__ (   self)

Definition at line 124 of file SingleAppInstance.py.

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

◆ __exit__()

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

Definition at line 128 of file SingleAppInstance.py.

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

◆ _readlock()

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

Definition at line 100 of file SingleAppInstance.py.

100  def _readlock(self):
101  '''Internal method to read lock info'''
102  try:
103  lock = {}
104  fh = open(self.path)
105  data = fh.read().rstrip().split('@')
106  fh.close()
107  lock['pid'], lock['host'] = data
108  return lock
109  except Exception:
110  return {'pid': 8**10, 'host': ''}
111 

◆ acquire()

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

Definition at line 37 of file SingleAppInstance.py.

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

◆ islocked()

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

Definition at line 112 of file SingleAppInstance.py.

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

◆ ownlock()

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

Definition at line 116 of file SingleAppInstance.py.

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

◆ release()

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

Definition at line 79 of file SingleAppInstance.py.

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

Member Data Documentation

◆ addr

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

Definition at line 23 of file SingleAppInstance.py.

◆ blocking

python.SingleAppInstance.SingleAppInstance.blocking

Definition at line 35 of file SingleAppInstance.py.

◆ debug

python.SingleAppInstance.SingleAppInstance.debug

Definition at line 32 of file SingleAppInstance.py.

◆ fddr

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

Definition at line 24 of file SingleAppInstance.py.

◆ host

python.SingleAppInstance.SingleAppInstance.host

Definition at line 30 of file SingleAppInstance.py.

◆ locked

python.SingleAppInstance.SingleAppInstance.locked

Definition at line 33 of file SingleAppInstance.py.

◆ lockfile

python.SingleAppInstance.SingleAppInstance.lockfile

Definition at line 34 of file SingleAppInstance.py.

◆ path

python.SingleAppInstance.SingleAppInstance.path

Definition at line 31 of file SingleAppInstance.py.

◆ pddr

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

Definition at line 25 of file SingleAppInstance.py.

◆ pid

python.SingleAppInstance.SingleAppInstance.pid

Definition at line 29 of file SingleAppInstance.py.


The documentation for this class was generated from the following file:
python.EventInfoMgtInit.release
release
Definition: EventInfoMgtInit.py:24
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:82
Muon::print
std::string print(const MuPatSegment &)
Definition: MuonTrackSteering.cxx:28
Trk::split
@ split
Definition: LayerMaterialProperties.h:38