Simplify lock context managers.

This commit is contained in:
Todd Gamblin
2015-09-17 01:05:19 -07:00
parent ccf311c9c6
commit e17ad6a684
10 changed files with 49 additions and 43 deletions

View File

@@ -29,13 +29,15 @@
import time
import socket
# Default timeout for locks.
DEFAULT_TIMEOUT = 60
class _ReadLockContext(object):
"""Context manager that takes and releases a read lock.
class Read_Lock_Instance(object):
"""
A context manager for getting shared access to the object lock
Arguments are lock and timeout (default 5 minutes)
"""
def __init__(self, lock, timeout=300):
def __init__(self, lock, timeout=DEFAULT_TIMEOUT):
self._lock = lock
self._timeout = timeout
@@ -46,12 +48,12 @@ def __exit__(self,type,value,traceback):
self._lock.release_read()
class Write_Lock_Instance(object):
"""
A context manager for getting exclusive access to the object lock
class _WriteLockContext(object):
"""Context manager that takes and releases a write lock.
Arguments are lock and timeout (default 5 minutes)
"""
def __init__(self, lock, timeout=300):
def __init__(self, lock, timeout=DEFAULT_TIMEOUT):
self._lock = lock
self._timeout = timeout
@@ -72,7 +74,17 @@ def __init__(self, file_path):
self._writes = 0
def acquire_read(self,timeout):
def write_lock(self, timeout=DEFAULT_TIMEOUT):
"""Convenience method that returns a write lock context."""
return _WriteLockContext(self, timeout)
def read_lock(self, timeout=DEFAULT_TIMEOUT):
"""Convenience method that returns a read lock context."""
return _ReadLockContext(self, timeout)
def acquire_read(self, timeout):
"""
Implements recursive lock. If held in both read and write mode,
the write lock will be maintained until all locks are released