Semaphore
SyncSemaphore
SyncSemaphore(
cache: CacheProtocol,
key: Any,
value: int = 1,
*,
timeout: float = DEFAULT_LOCK_TIMEOUT,
expire: float | None = None,
tags: str | Iterable[str] | None = None
)
Bases: SyncSemaphoreProtocol
Synchronous semaphore implementation using spin-lock algorithm.
Assumes the key will not be evicted. Set the eviction policy to 'none' on the cache to guarantee the key is not evicted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
CacheProtocol
|
Cache to use for semaphore. |
required |
|
Any
|
Key for semaphore. |
required |
|
int
|
Value for semaphore. |
1
|
|
float
|
Timeout for semaphore. |
DEFAULT_LOCK_TIMEOUT
|
|
float | None
|
Expiration time for semaphore. |
None
|
|
str | Iterable[str] | None
|
Tags for semaphore. |
None
|
Examples:
Source code in src/typed_diskcache/implement/sync/semaphore.py
acquire
Acquire semaphore by decrementing value using spin-lock algorithm.
Source code in src/typed_diskcache/implement/sync/semaphore.py
release
Release semaphore by incrementing value.
Source code in src/typed_diskcache/implement/sync/semaphore.py
AsyncSemaphore
AsyncSemaphore(
cache: CacheProtocol,
key: Any,
value: int = 1,
*,
timeout: float = DEFAULT_LOCK_TIMEOUT,
expire: float | None = None,
tags: str | Iterable[str] | None = None
)
Bases: AsyncSemaphoreProtocol
Asynchronous semaphore implementation using spin-lock algorithm.
Warning
If the current Python interpreter is free-threading (without GIL),
using AsyncSemaphore may lead to unexpected
behavior. Consider using SyncSemaphore
instead in such cases.
Assumes the key will not be evicted. Set the eviction policy to 'none' on the cache to guarantee the key is not evicted.
Asynchronous version of SyncSemaphore.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
CacheProtocol
|
Cache to use for semaphore. |
required |
|
Any
|
Key for semaphore. |
required |
|
int
|
Value for semaphore. |
1
|
|
float
|
Timeout for semaphore. |
DEFAULT_LOCK_TIMEOUT
|
|
float | None
|
Expiration time for semaphore. |
None
|
|
str | Iterable[str] | None
|
Tags for semaphore. |
None
|
Warns:
| Type | Description |
|---|---|
RuntimeWarning
|
If the current Python interpreter is free-threading (without GIL),
using |
Examples:
Source code in src/typed_diskcache/implement/sync/semaphore.py
acquire
async
Acquire semaphore by decrementing value using spin-lock algorithm.
Source code in src/typed_diskcache/implement/sync/semaphore.py
release
async
Release semaphore by incrementing value.