summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbwarsaw1998-07-06 15:46:58 +0000
committerbwarsaw1998-07-06 15:46:58 +0000
commit5308be6e2ff8a5f1944f3d04fdd0306fbcfe7f7f (patch)
tree9572a403e1ac9b6c3c33ae1903b2d8f9bf5ce5ea
parent288f6ccf6eee9208eeb02e4dd4745a8c47e97606 (diff)
downloadmailman-5308be6e2ff8a5f1944f3d04fdd0306fbcfe7f7f.tar.gz
mailman-5308be6e2ff8a5f1944f3d04fdd0306fbcfe7f7f.tar.zst
mailman-5308be6e2ff8a5f1944f3d04fdd0306fbcfe7f7f.zip
-rw-r--r--Mailman/LockFile.py140
-rw-r--r--Mailman/flock.py140
2 files changed, 152 insertions, 128 deletions
diff --git a/Mailman/LockFile.py b/Mailman/LockFile.py
index 53715ad34..e4d4ef3ee 100644
--- a/Mailman/LockFile.py
+++ b/Mailman/LockFile.py
@@ -37,69 +37,81 @@ NotLockedError = "NotLockedError"
TimeOutError = "TimeOutError"
class FileLock:
- def __init__(self, lockfile, hung_timeout = DEFAULT_HUNG_TIMEOUT,
- sleep_interval = DEFAULT_SLEEP_INTERVAL):
- self.lockfile = lockfile
- self.hung_timeout = hung_timeout
- self.sleep_interval = sleep_interval
- self.tmpfname = "%s.%s.%d" % (lockfile, socket.gethostname(), os.getpid())
- self.is_locked = 0
- if not os.path.exists(self.lockfile):
- try:
- file = open(self.lockfile, "w+")
- except IOError:
- pass
+ def __init__(self, lockfile, hung_timeout = DEFAULT_HUNG_TIMEOUT,
+ sleep_interval = DEFAULT_SLEEP_INTERVAL):
+ self.lockfile = lockfile
+ self.hung_timeout = hung_timeout
+ self.sleep_interval = sleep_interval
+ self.tmpfname = "%s.%s.%d" % (lockfile, socket.gethostname(),
+ os.getpid())
+ self.is_locked = 0
+ if not os.path.exists(self.lockfile):
+ try:
+ file = open(self.lockfile, "w+")
+ except IOError:
+ pass
- # Note that no one new can grab the lock once we've opened our
- # tmpfile until we close it, even if we don't have the lock. So
- # checking the PID and stealing the lock are garunteed to be atomic.
- def lock(self, timeout = 0):
- """Blocks until the lock can be obtained. Raises a TimeOutError
- exception if a positive timeout value is given and that time
- elapses before the lock is obtained.
- """
- if timeout > 0:
- timeout_time = time.time() + timeout
- last_pid = -1
- if self.locked():
- raise AlreadyCalledLockError
- while 1:
- os.link(self.lockfile, self.tmpfname)
- if os.stat(self.tmpfname)[3] == 2:
- file = open(self.tmpfname, 'w+')
- file.write(`os.getpid(),self.tmpfname`)
- file.close()
- self.is_locked = 1
- break
- if timeout and timeout_time < time.time():
- raise TimeOutError
- file = open(self.tmpfname, 'r')
- try:
- pid,winner = eval(file.read())
- except SyntaxError: # no info in file... *can* happen
- file.close()
- os.unlink(self.tmpfname)
- continue
- file.close()
- if pid <> last_pid:
- last_pid = pid
- stime = time.time()
- if (stime + self.hung_timeout < time.time()) and self.hung_timeout > 0:
- file = open(self.tmpfname, 'w+')
- file.write(`os.getpid(),self.tmpfname`)
- try:
- os.unlink(winner)
- except os.error:
- pass
+ # Note that no one new can grab the lock once we've opened our tmpfile
+ # until we close it, even if we don't have the lock. So checking the PID
+ # and stealing the lock are garunteed to be atomic.
+ def lock(self, timeout = 0):
+ """Blocks until the lock can be obtained.
+
+ Raises a TimeOutError exception if a positive timeout value is given
+ and that time elapses before the lock is obtained.
+
+ """
+ if timeout > 0:
+ timeout_time = time.time() + timeout
+ last_pid = -1
+ if self.locked():
+ raise AlreadyCalledLockError
+ while 1:
+ os.link(self.lockfile, self.tmpfname)
+ if os.stat(self.tmpfname)[3] == 2:
+ file = open(self.tmpfname, 'w+')
+ file.write(`os.getpid(),self.tmpfname`)
+ file.close()
+ self.is_locked = 1
+ break
+ if timeout and timeout_time < time.time():
+ raise TimeOutError
+ file = open(self.tmpfname, 'r')
+ try:
+ pid,winner = eval(file.read())
+ except SyntaxError: # no info in file... *can* happen
+ file.close()
+ os.unlink(self.tmpfname)
+ continue
+ file.close()
+ if pid <> last_pid:
+ last_pid = pid
+ stime = time.time()
+ if (stime + self.hung_timeout < time.time()) and \
+ self.hung_timeout > 0:
+ # then
+ file = open(self.tmpfname, 'w+')
+ file.write(`os.getpid(),self.tmpfname`)
+ try:
+ os.unlink(winner)
+ except os.error:
+ pass
+ try:
+ os.unlink(self.tmpfname)
+ except os.error, (errno, msg):
+ # XXX: better debugging
+ msg = msg + ': ' + self.tmpfname
+ raise os.error, (errno, msg)
+ continue
+ os.unlink(self.tmpfname)
+ time.sleep(self.sleep_interval)
+
+ # This could error if the lock is stolen. You must catch it.
+ def unlock(self):
+ if not self.locked():
+ raise NotLockedError
+ self.is_locked = 0
os.unlink(self.tmpfname)
- continue
- os.unlink(self.tmpfname)
- time.sleep(self.sleep_interval)
- # This could error if the lock is stolen. You must catch it.
- def unlock(self):
- if not self.locked():
- raise NotLockedError
- self.is_locked = 0
- os.unlink(self.tmpfname)
- def locked(self):
- return os.path.exists(self.tmpfname) and self.is_locked \ No newline at end of file
+
+ def locked(self):
+ return os.path.exists(self.tmpfname) and self.is_locked
diff --git a/Mailman/flock.py b/Mailman/flock.py
index 53715ad34..e4d4ef3ee 100644
--- a/Mailman/flock.py
+++ b/Mailman/flock.py
@@ -37,69 +37,81 @@ NotLockedError = "NotLockedError"
TimeOutError = "TimeOutError"
class FileLock:
- def __init__(self, lockfile, hung_timeout = DEFAULT_HUNG_TIMEOUT,
- sleep_interval = DEFAULT_SLEEP_INTERVAL):
- self.lockfile = lockfile
- self.hung_timeout = hung_timeout
- self.sleep_interval = sleep_interval
- self.tmpfname = "%s.%s.%d" % (lockfile, socket.gethostname(), os.getpid())
- self.is_locked = 0
- if not os.path.exists(self.lockfile):
- try:
- file = open(self.lockfile, "w+")
- except IOError:
- pass
+ def __init__(self, lockfile, hung_timeout = DEFAULT_HUNG_TIMEOUT,
+ sleep_interval = DEFAULT_SLEEP_INTERVAL):
+ self.lockfile = lockfile
+ self.hung_timeout = hung_timeout
+ self.sleep_interval = sleep_interval
+ self.tmpfname = "%s.%s.%d" % (lockfile, socket.gethostname(),
+ os.getpid())
+ self.is_locked = 0
+ if not os.path.exists(self.lockfile):
+ try:
+ file = open(self.lockfile, "w+")
+ except IOError:
+ pass
- # Note that no one new can grab the lock once we've opened our
- # tmpfile until we close it, even if we don't have the lock. So
- # checking the PID and stealing the lock are garunteed to be atomic.
- def lock(self, timeout = 0):
- """Blocks until the lock can be obtained. Raises a TimeOutError
- exception if a positive timeout value is given and that time
- elapses before the lock is obtained.
- """
- if timeout > 0:
- timeout_time = time.time() + timeout
- last_pid = -1
- if self.locked():
- raise AlreadyCalledLockError
- while 1:
- os.link(self.lockfile, self.tmpfname)
- if os.stat(self.tmpfname)[3] == 2:
- file = open(self.tmpfname, 'w+')
- file.write(`os.getpid(),self.tmpfname`)
- file.close()
- self.is_locked = 1
- break
- if timeout and timeout_time < time.time():
- raise TimeOutError
- file = open(self.tmpfname, 'r')
- try:
- pid,winner = eval(file.read())
- except SyntaxError: # no info in file... *can* happen
- file.close()
- os.unlink(self.tmpfname)
- continue
- file.close()
- if pid <> last_pid:
- last_pid = pid
- stime = time.time()
- if (stime + self.hung_timeout < time.time()) and self.hung_timeout > 0:
- file = open(self.tmpfname, 'w+')
- file.write(`os.getpid(),self.tmpfname`)
- try:
- os.unlink(winner)
- except os.error:
- pass
+ # Note that no one new can grab the lock once we've opened our tmpfile
+ # until we close it, even if we don't have the lock. So checking the PID
+ # and stealing the lock are garunteed to be atomic.
+ def lock(self, timeout = 0):
+ """Blocks until the lock can be obtained.
+
+ Raises a TimeOutError exception if a positive timeout value is given
+ and that time elapses before the lock is obtained.
+
+ """
+ if timeout > 0:
+ timeout_time = time.time() + timeout
+ last_pid = -1
+ if self.locked():
+ raise AlreadyCalledLockError
+ while 1:
+ os.link(self.lockfile, self.tmpfname)
+ if os.stat(self.tmpfname)[3] == 2:
+ file = open(self.tmpfname, 'w+')
+ file.write(`os.getpid(),self.tmpfname`)
+ file.close()
+ self.is_locked = 1
+ break
+ if timeout and timeout_time < time.time():
+ raise TimeOutError
+ file = open(self.tmpfname, 'r')
+ try:
+ pid,winner = eval(file.read())
+ except SyntaxError: # no info in file... *can* happen
+ file.close()
+ os.unlink(self.tmpfname)
+ continue
+ file.close()
+ if pid <> last_pid:
+ last_pid = pid
+ stime = time.time()
+ if (stime + self.hung_timeout < time.time()) and \
+ self.hung_timeout > 0:
+ # then
+ file = open(self.tmpfname, 'w+')
+ file.write(`os.getpid(),self.tmpfname`)
+ try:
+ os.unlink(winner)
+ except os.error:
+ pass
+ try:
+ os.unlink(self.tmpfname)
+ except os.error, (errno, msg):
+ # XXX: better debugging
+ msg = msg + ': ' + self.tmpfname
+ raise os.error, (errno, msg)
+ continue
+ os.unlink(self.tmpfname)
+ time.sleep(self.sleep_interval)
+
+ # This could error if the lock is stolen. You must catch it.
+ def unlock(self):
+ if not self.locked():
+ raise NotLockedError
+ self.is_locked = 0
os.unlink(self.tmpfname)
- continue
- os.unlink(self.tmpfname)
- time.sleep(self.sleep_interval)
- # This could error if the lock is stolen. You must catch it.
- def unlock(self):
- if not self.locked():
- raise NotLockedError
- self.is_locked = 0
- os.unlink(self.tmpfname)
- def locked(self):
- return os.path.exists(self.tmpfname) and self.is_locked \ No newline at end of file
+
+ def locked(self):
+ return os.path.exists(self.tmpfname) and self.is_locked