ding.utils.lock_helper¶
ding.utils.lock_helper
¶
LockContextType
¶
Bases: Enum
Overview
Enum to express the type of the lock.
LockContext
¶
Bases: object
Overview
Generate a LockContext in order to make sure the thread safety.
Interfaces
__init__, __enter__, __exit__.
Example
with LockContext() as lock: print("Do something here.")
__init__(lock_type=LockContextType.THREAD_LOCK)
¶
Overview
Init the lock according to the given type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
- lock_type (
|
obj: |
required |
acquire()
¶
Overview
Acquires the lock.
release()
¶
Overview
Releases the lock.
__enter__()
¶
Overview
Enters the context and acquires the lock.
__exit__(*args, **kwargs)
¶
Overview
Exits the context and releases the lock.
Arguments:
- args (:obj:Tuple): The arguments passed to the __exit__ function.
- kwargs (:obj:Dict): The keyword arguments passed to the __exit__ function.
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
__init__(lock_path)
¶
Overview
Initialize the LockHelper object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
- lock_path (
|
obj: |
required |
__enter__()
¶
Overview
Acquires the lock and opens the lock file in write mode. If the lock file does not exist, it is created.
__exit__(*args, **kwargs)
¶
Overview
Closes the file and releases any resources used by the lock_helper object.
Arguments:
- args (:obj:Tuple): The arguments passed to the __exit__ function.
- kwargs (:obj:Dict): The keyword arguments passed to the __exit__ function.
get_rw_file_lock(name, op)
¶
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.
Returns:
- (:obj:RWLockFairD): Generated rwlock
get_file_lock(name, op)
¶
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.
Full Source Code
../ding/utils/lock_helper.py