Skip to content

Commit 2dfcddb

Browse files
committed
Initial work toward adopting zc.lockfile. Ref #23.
1 parent aea69ce commit 2dfcddb

File tree

2 files changed

+12
-26
lines changed

2 files changed

+12
-26
lines changed

pytest_services/locks.py

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,41 +38,26 @@ def lock_file(filename, content, operation):
3838
return handle
3939

4040

41-
def unlock_file(filename, handle, remove=True):
42-
"""Unlock given file."""
43-
fcntl.flock(handle, fcntl.LOCK_UN)
44-
handle.close()
45-
if remove:
46-
try:
47-
os.unlink(filename)
48-
except OSError:
49-
pass
41+
42+
def try_remove(filename):
43+
try:
44+
os.unlink(filename)
45+
except OSError:
46+
pass
5047

5148

5249
@contextlib.contextmanager
53-
def file_lock(filename, operation=fcntl.LOCK_EX, remove=True, timeout=20):
50+
def file_lock(filename, remove=True, timeout=20):
5451
"""A lock that is shared across processes.
5552
5653
:param filename: the name of the file that will be locked.
57-
:pram operation: the lock operation.
5854
5955
"""
60-
# http://bservices_log.vmfarms.com/2011/03/cross-process-locking-and.html
61-
times = 0
62-
while True:
63-
try:
64-
handle = lock_file(filename, None, operation)
65-
break
66-
except IOError:
67-
if times > timeout:
68-
raise Exception('Not able to aquire lock file {0} in {1}'.format(filename, timeout))
69-
time.sleep(0.5)
70-
times += 1
56+
with contextlib.closing(zc.lockfile.LockFile(filename)) as lockfile:
57+
yield lockfile._fp
7158

72-
try:
73-
yield handle
74-
finally:
75-
unlock_file(filename, handle, remove=remove)
59+
60+
remove and try_remove(filename)
7661

7762

7863
def unlock_resource(name, resource, lock_dir, services_log):

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
'requests',
2020
'psutil',
2121
'pytest',
22+
'zc.lockfile',
2223
]
2324

2425
PY2 = sys.version_info[0] < 3

0 commit comments

Comments
 (0)