bot.utils.lock#

Lock resources.

Module Contents#

Classes#

SharedEvent

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

Functions#

lock(→ collections.abc.Callable)

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

lock_arg(→ collections.abc.Callable)

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

Attributes#

bot.utils.lock.log[source]#
bot.utils.lock.__lock_dicts[source]#
bot.utils.lock._IdCallableReturn[source]#
bot.utils.lock._IdCallable[source]#
bot.utils.lock.ResourceId[source]#
class bot.utils.lock.SharedEvent[source]#

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__() None[source]#

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

__exit__(_exc_type, _exc_val, _exc_tb) None[source]#

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

async wait() None[source]#

Wait for all active holders to exit.

bot.utils.lock.lock(namespace: collections.abc.Hashable, resource_id: ResourceId, *, raise_error: bool = False, wait: bool = False) collections.abc.Callable[source]#

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

If wait is 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.

If raise_error is True, raise LockedResourceError if the lock cannot be acquired.

namespace is an identifier used to prevent collisions among resource IDs.

resource_id 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.

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

bot.utils.lock.lock_arg(namespace: collections.abc.Hashable, name_or_pos: bot.utils.function.Argument, func: collections.abc.Callable[[Any], _IdCallableReturn] | None = None, *, raise_error: bool = False, wait: bool = False) collections.abc.Callable[source]#

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

func is an optional callable or awaitable which will return the ID given the argument value. See lock docs for more information.