lock

exception LockedResourceError(resource_type, resource_id)[source]

Bases: RuntimeError

Exception raised when an operation is attempted on a locked resource.

type[source]

Name of the locked resource’s type

Type:

str

id[source]

ID of the locked resource

Type:

Hashable

__init__(resource_type, resource_id)[source]
class SharedEvent[source]

Bases: object

Context manager managing an internal event exposed through the wait coro.

While any code is executing in this context manager, the underlying event will not be set; when all of the holders finish the event will be set.

__enter__()[source]

Increment the count of the active holders and clear the internal event.

__exit__(_exc_type, _exc_val, _exc_tb)[source]

Decrement the count of the active holders; if 0 is reached set the internal event.

__init__()[source]
async wait()[source]

Wait for all active holders to exit.

Return type:

None

lock(namespace, resource_id, *, raise_error=False, wait=False)[source]

Turn the decorated coroutine function into a mutually exclusive operation on a resource_id.

If decorating a command, this decorator must go before (below) the command decorator.

Parameters:
  • namespace (Hashable) – An identifier used to prevent collisions among resource IDs.

  • resource_id (Hashable | Callable[[OrderedDict[str, Any]], Hashable | Awaitable[Hashable]]) – identifies a resource on which to perform a mutually exclusive operation. It may also be a callable or awaitable which will return the resource ID given an ordered mapping of the parameters’ names to arguments’ values.

  • raise_error (bool) – If True, raise LockedResourceError if the lock cannot be acquired.

  • wait (bool) – If True, wait until the lock becomes available. Otherwise, if any other mutually exclusive function currently holds the lock for a resource, do not run the decorated function and return None.

Raises:

LockedResourceError – If the lock can’t be acquired and raise_error is set to True.

Return type:

Callable

lock_arg(namespace, name_or_pos, func=None, *, raise_error=False, wait=False)[source]

Apply the lock decorator using the value of the arg at the given name/position as the ID.

See lock docs for more information.

Parameters:

func (Callable[[Any], Hashable | Awaitable[Hashable]] | None) – An optional callable or awaitable which will return the ID given the argument value.

Return type:

Callable