Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test refactor, timeout=0 fix, and PIDLockFile fix #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lockfile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,10 @@ class NotMyLock(UnlockError):
"""
pass

class LockBase:

class LockBase(object):
"""Base class for platform-specific lock classes."""

def __init__(self, path, threaded=True, timeout=None):
"""
>>> lock = LockBase('somefile')
Expand All @@ -174,10 +176,10 @@ def __init__(self, path, threaded=True, timeout=None):
else:
self.tname = ""
dirname = os.path.dirname(self.lock_file)
self.unique_id = "%s.%s" % (self.tname, self.pid)
self.unique_name = os.path.join(dirname,
"%s%s.%s" % (self.hostname,
self.tname,
self.pid))
"%s%s" % (self.hostname,
self.unique_id))
self.timeout = timeout

def acquire(self, timeout=None):
Expand Down
4 changes: 2 additions & 2 deletions lockfile/linklockfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from . import (LockBase, LockFailed, NotLocked, NotMyLock, LockTimeout,
AlreadyLocked)


class LinkLockFile(LockBase):
"""Lock access to a file using atomic property of link(2).

Expand All @@ -19,7 +20,7 @@ def acquire(self, timeout=None):
except IOError:
raise LockFailed("failed to create %s" % self.unique_name)

timeout = timeout is not None and timeout or self.timeout
timeout = timeout if timeout is not None else self.timeout
end_time = time.time()
if timeout is not None and timeout > 0:
end_time += timeout
Expand Down Expand Up @@ -70,4 +71,3 @@ def i_am_locking(self):
def break_lock(self):
if os.path.exists(self.lock_file):
os.unlink(self.lock_file)

2 changes: 1 addition & 1 deletion lockfile/mkdirlockfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __init__(self, path, threaded=True, timeout=None):
self.pid))

def acquire(self, timeout=None):
timeout = timeout is not None and timeout or self.timeout
timeout = timeout if timeout is not None else self.timeout
end_time = time.time()
if timeout is not None and timeout > 0:
end_time += timeout
Expand Down
19 changes: 8 additions & 11 deletions lockfile/pidlockfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
from __future__ import absolute_import

import os
import sys
import errno
import time

from . import (LockBase, AlreadyLocked, LockFailed, NotLocked, NotMyLock,
LockTimeout)


class PIDLockFile(LockBase):
""" Lockfile implemented as a Unix PID file.

Expand All @@ -37,10 +36,7 @@ class PIDLockFile(LockBase):
def __init__(self, path, threaded=False, timeout=None):
# pid lockfiles don't support threaded operation, so always force
# False as the threaded arg.
LockBase.__init__(self, path, False, timeout)
dirname = os.path.dirname(self.lock_file)
basename = os.path.split(self.path)[-1]
self.unique_name = self.path
super(PIDLockFile, self).__init__(path, False, timeout)

def read_pid(self):
""" Get the PID from the lock file.
Expand Down Expand Up @@ -69,8 +65,10 @@ def acquire(self, timeout=None):
Creates the PID file for this lock, or raises an error if
the lock could not be acquired.
"""
if self.i_am_locking():
return

timeout = timeout is not None and timeout or self.timeout
timeout = timeout if timeout is not None else self.timeout
end_time = time.time()
if timeout is not None and timeout > 0:
end_time += timeout
Expand Down Expand Up @@ -132,10 +130,10 @@ def read_pid_from_pidfile(pidfile_path):
pass
else:
# According to the FHS 2.3 section on PID files in /var/run:
#
#
# The file must consist of the process identifier in
# ASCII-encoded decimal, followed by a newline character.
#
#
# Programs that read PID files should be somewhat flexible
# in what they accept; i.e., they should ignore extra
# whitespace, leading zeroes, absence of the trailing
Expand Down Expand Up @@ -170,8 +168,7 @@ def write_pid_to_pidfile(pidfile_path):
# example, if crond was process number 25, /var/run/crond.pid
# would contain three characters: two, five, and newline.

pid = os.getpid()
line = "%(pid)d\n" % vars()
line = "%d\n" % os.getpid()
pidfile.write(line)
pidfile.close()

Expand Down
9 changes: 5 additions & 4 deletions lockfile/sqlitelockfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from . import LockBase, NotLocked, NotMyLock, LockTimeout, AlreadyLocked


class SQLiteLockFile(LockBase):
"Demonstrate SQL-based locking."

Expand All @@ -34,7 +35,7 @@ def __init__(self, path, threaded=True, timeout=None):

import sqlite3
self.connection = sqlite3.connect(SQLiteLockFile.testdb)

c = self.connection.cursor()
try:
c.execute("create table locks"
Expand All @@ -50,7 +51,7 @@ def __init__(self, path, threaded=True, timeout=None):
atexit.register(os.unlink, SQLiteLockFile.testdb)

def acquire(self, timeout=None):
timeout = timeout is not None and timeout or self.timeout
timeout = timeout if timeout is not None else self.timeout
end_time = time.time()
if timeout is not None and timeout > 0:
end_time += timeout
Expand Down Expand Up @@ -97,7 +98,7 @@ def acquire(self, timeout=None):
if len(rows) == 1:
# We're the locker, so go home.
return

# Maybe we should wait a bit longer.
if timeout is not None and time.time() > end_time:
if timeout > 0:
Expand Down Expand Up @@ -130,7 +131,7 @@ def _who_is_locking(self):
" where lock_file = ?",
(self.lock_file,))
return cursor.fetchone()[0]

def is_locked(self):
cursor = self.connection.cursor()
cursor.execute("select * from locks"
Expand Down
2 changes: 1 addition & 1 deletion lockfile/symlinklockfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def acquire(self, timeout=None):
# open(self.unique_name, "wb").close()
#except IOError:
# raise LockFailed("failed to create %s" % self.unique_name)
timeout = timeout is not None and timeout or self.timeout
timeout = timeout if timeout is not None else self.timeout
end_time = time.time()
if timeout is not None and timeout > 0:
end_time += timeout
Expand Down
Loading