Dex.Limiter¶
- Subclasses:
None
Methods¶
- Inherited:
class |
|
|
|
|
|
|
|
|
|
|
|
|
Virtual Methods¶
None
Fields¶
None
Class Details¶
- class Dex.Limiter¶
- Bases:
- Abstract:
No
DexLimiterlimits the number of operations running concurrently.A limiter starts with a fixed number of permits. Use [method`Dex`.Limiter.acquire] and [method`Dex`.Limiter.release] directly when a permit must cover a custom scope, or use [method`Dex`.Limiter.run] or [method`Dex`.Limiter.run_on_pool] to acquire a permit and release it automatically when the work completes.
Added in version 1.2.
- classmethod new(max_concurrency)¶
- Parameters:
max_concurrency (
int) – the maximum number of concurrent operations- Returns:
a new
DexLimiter- Return type:
Creates a new
DexLimiterwith max_concurrency permits.max_concurrency must be greater than zero. Each successful acquisition consumes one permit until [method`Dex`.Limiter.release] is called.
Added in version 1.2.
- acquire()¶
- Returns:
a future that resolves when a permit is acquired
- Return type:
Acquires one permit from self.
The returned future resolves to
Truewhen a permit has been acquired. Call [method`Dex`.Limiter.release] exactly once for each resolved acquisition.If the returned future is discarded before the permit is acquired, the permit is returned to the limiter when it becomes available. If self is closed before acquisition completes, the returned future rejects with
Dex.Error.SEMAPHORE_CLOSED.Added in version 1.2.
- close()¶
Closes self.
Pending and future acquisitions reject with
Dex.Error.SEMAPHORE_CLOSED. Permits already acquired remain valid, but releasing them after close will not make them available for new work.Added in version 1.2.
- close_after_drain()¶
- Returns:
a [class`Dex`.Future] that resolves to
true- Return type:
Closes self and waits for all queued and running work to complete.
After this function is called, new acquire attempts are rejected with
Dex.Error.SEMAPHORE_CLOSED.The returned future resolves to
%TRUEonce all outstanding pending acquire futures and held permits are complete. Existing permit holders must still eventually release.Added in version 1.2.
- get_max_concurrency()¶
- Returns:
the maximum number of concurrent operations
- Return type:
Gets the maximum number of permits available from self.
Added in version 1.2.
- release()¶
Releases one permit previously acquired from self.
This must be called exactly once for each successful [method`Dex`.Limiter.acquire] unless the permit is managed by [method`Dex`.Limiter.run].
Added in version 1.2.
- run(scheduler, stack_size, func, *func_data)¶
- Parameters:
scheduler (
Dex.SchedulerorNone) – scheduler to spawn func on, orNonefor the thread defaultstack_size (
int) – stack size for the spawned fiber, or zero to use the defaultfunc (
Dex.FiberFunc) – fiber function to run after a permit is acquired
- Returns:
a future representing the spawned fiber
- Return type:
Runs func while holding one permit from self.
The returned future resolves or rejects with the result of the spawned fiber. The permit is released automatically after the fiber resolves or rejects. If the returned future is discarded after the fiber starts, the fiber is allowed to complete so that the permit can be released.
Added in version 1.2.
- run_coroutine(scheduler, func, *user_data)¶
- Parameters:
scheduler (
Dex.SchedulerorNone) – scheduler to spawn func on, orNonefor the thread defaultfunc (
Dex.CoroutineFunc) – coroutine function to run after a permit is acquired
- Returns:
a future representing the spawned coroutine
- Return type:
Runs func while holding one permit from self.
The returned future resolves or rejects with the result of the spawned coroutine. The permit is released automatically after the coroutine resolves or rejects. If the returned future is discarded after the coroutine starts, the coroutine is allowed to complete so that the permit can be released.
Added in version 1.2.
- run_on_pool(pool, thread_func, *user_data)¶
- Parameters:
pool (
Dex.ThreadPool) – aDexThreadPoolthread_func (
Dex.ThreadFunc) – function to run on pool after a permit is acquired
- Returns:
a future representing the submitted work
- Return type:
Runs thread_func on pool while holding one permit from self.
The returned future resolves or rejects with the result of the submitted thread-pool work. The permit is released automatically after the work resolves or rejects. If the returned future is discarded after the work is submitted to pool, the work is allowed to complete so that the permit can be released.
Workers in
DexThreadPoolare not scheduler threads, so thread_func must not usedex_await().Added in version 1.2.