Skip to content

Commit

Permalink
polish(rjy): polish comments in normalizer_helper and lock_helper (#752)
Browse files Browse the repository at this point in the history
* polish(rjy): polish comments in normalizer_helper

* polish(rjy): polish the comments in lock_helper

* polish(rjy): polish according to comments

* polish(rjy): polish typelint

* polish(rjy): fix format

* polish(rjy): polish the typelint of return

* polish(rjy): fix yapf
  • Loading branch information
nighood authored Dec 11, 2023
1 parent 4f8f82a commit 5788265
Show file tree
Hide file tree
Showing 2 changed files with 292 additions and 53 deletions.
74 changes: 62 additions & 12 deletions ding/utils/lock_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
@unique
class LockContextType(Enum):
"""
Enum to express the type of the lock
Overview:
Enum to express the type of the lock.
"""
THREAD_LOCK = 1
PROCESS_LOCK = 2
Expand All @@ -32,37 +33,48 @@ class LockContext(object):
Generate a LockContext in order to make sure the thread safety.
Interfaces:
``__init__``, ``__enter__``, ``__exit__``
``__init__``, ``__enter__``, ``__exit__``.
Example:
>>> with LockContext() as lock:
>>> print("Do something here.")
"""

def __init__(self, type_: LockContextType = LockContextType.THREAD_LOCK):
r"""
"""
Overview:
Init the lock according to given type
Init the lock according to the given type.
Arguments:
type_ (:obj:`LockContextType`): The type of lock to be used. Defaults to LockContextType.THREAD_LOCK.
"""
self.lock = _LOCK_TYPE_MAPPING[type_]()

def acquire(self):
"""
Overview:
Acquires the lock.
"""
self.lock.acquire()

def release(self):
"""
Overview:
Releases the lock.
"""
self.lock.release()

def __enter__(self):
"""
Overview:
Entering the context and acquire lock
Enters the context and acquires the lock.
"""
self.lock.acquire()

def __exit__(self, *args, **kwargs):
"""
Overview:
Quiting the context and release lock
Exits the context and releases the lock.
"""
self.lock.release()

Expand All @@ -71,15 +83,15 @@ def __exit__(self, *args, **kwargs):


def get_rw_file_lock(name: str, op: str):
r'''
"""
Overview:
Get generated file lock with name and operator
Arguments:
- name (:obj:`str`) Lock's name.
- op (:obj:`str`) Assigned operator, i.e. ``read`` or ``write``.
- name (:obj:`str`): Lock's name.
- op (:obj:`str`): Assigned operator, i.e. ``read`` or ``write``.
Returns:
- (:obj:`RWLockFairD`) Generated rwlock
'''
- (:obj:`RWLockFairD`): Generated rwlock
"""
assert op in ['read', 'write']
try:
from readerwriterlock import rwlock
Expand All @@ -98,22 +110,60 @@ def get_rw_file_lock(name: str, op: str):


class FcntlContext:
"""
Overview:
A context manager that acquires an exclusive lock on a file using fcntl. \
This is useful for preventing multiple processes from running the same code.
Interfaces:
``__init__``, ``__enter__``, ``__exit__``.
Example:
>>> lock_path = "/path/to/lock/file"
>>>with FcntlContext(lock_path) as lock:
>>> # Perform operations while the lock is held
"""

def __init__(self, lock_path: str) -> None:
"""
Overview:
Initialize the LockHelper object.
Arguments:
- lock_path (:obj:`str`): The path to the lock file.
"""
self.lock_path = lock_path
self.f = None

def __enter__(self) -> None:
"""
Overview:
Acquires the lock and opens the lock file in write mode. \
If the lock file does not exist, it is created.
"""
assert self.f is None, self.lock_path
self.f = open(self.lock_path, 'w')
fcntl.flock(self.f.fileno(), fcntl.LOCK_EX)

def __exit__(self, *args, **kwargs) -> None:
"""
Overview:
Closes the file and releases any resources used by the lock_helper object.
"""
self.f.close()
self.f = None


def get_file_lock(name: str, op: str) -> None:
def get_file_lock(name: str, op: str) -> FcntlContext:
"""
Overview:
Acquires a file lock for the specified file. \
Arguments:
- name (:obj:`str`): The name of the file.
- op (:obj:`str`): The operation to perform on the file lock.
"""
if fcntl is None:
return get_rw_file_lock(name, op)
else:
Expand Down
Loading

0 comments on commit 5788265

Please sign in to comment.