Go to the latest version of this document.

# ``twisted`.`internet`.`defer`.`DeferredSemaphore``

class documentation

`class DeferredSemaphore(_ConcurrencyPrimitive): (source)`

Constructor: `DeferredSemaphore(tokens)`

View In Hierarchy

A semaphore for event driven systems.

If you are looking into this as a means of limiting parallelism, you might find `twisted.internet.task.Cooperator` more useful.

Method | `__init__` | No summary
Method | `acquire` | Attempt to acquire the token.
Method | `release` | Release the token.
Instance Variable | `limit` | At most this many users may acquire this semaphore at once.
Instance Variable | `tokens` | The difference between `limit` and the number of users which have currently acquired this semaphore.
Method | `_cancelAcquire` | Remove a deferred d from our waiting list, as the deferred has been canceled.

Inherited from `_ConcurrencyPrimitive`:

Method | `__aenter__` | We can be used as an asynchronous context manager.
Method | `__aexit__` | Undocumented
Method | `run` | Acquire, run, release.
Instance Variable | `waiting` | Undocumented
Method | `_releaseAndReturn` | Undocumented

def __init__(self, tokens: `int`): (source)
overrides `twisted.internet.defer._ConcurrencyPrimitive.__init__`

Parameters
tokens:`int` | initial value of `tokens` and `limit`

def acquire(self: `Self`) -> `Deferred[Self]`: (source)
overrides `twisted.internet.defer._ConcurrencyPrimitive.acquire`

Attempt to acquire the token.

Returns
`Deferred[Self]` | a `Deferred` which fires on token acquisition.

def release(self: `Self`): (source)
overrides `twisted.internet.defer._ConcurrencyPrimitive.release`

Release the token.

Should be called by whoever did the `acquire`() when the shared resource is free.

limit = (source)

At most this many users may acquire this semaphore at once.

tokens = (source)

The difference between `limit` and the number of users which have currently acquired this semaphore.

def _cancelAcquire(self: `Self`, d: `Deferred[Self]`): (source)

Remove a deferred d from our waiting list, as the deferred has been canceled.

Note: We do not need to wrap this in a try/except to catch d not being in self.waiting because this canceller will not be called if d has fired. release() pops a deferred out of self.waiting and calls it, so the canceller will no longer be called.

Parameters
d:`Deferred[Self]` | The deferred that has been canceled.