pydispatch.aioutils

AioWeakMethodContainer class

class AioWeakMethodContainer[source]

Bases: WeakMethodContainer

Storage for coroutine functions as weak references

Added in version 0.1.0.

__call__(*args, **kwargs)[source]

Triggers all stored callbacks (coroutines)

Parameters:
  • *args – Positional arguments to pass to callbacks

  • **kwargs – Keyword arguments to pass to callbacks

add_method(callback, loop: AbstractEventLoop | None = None, **kwargs)[source]

Add a coroutine function

Parameters:

Important

The positional arguments are now callback, loop, rather than loop, callback. Calls using both keywords are unchanged.

Changed in version 0.3.0: Reordered the arguments and accepted extra keyword arguments.

iter_instances()[source]

Iterate over the stored objects

iter_methods()[source]

Iterate over stored coroutine functions

Yields:

Stored coroutine function objects

submit_coroutine(coro, loop)[source]

Schedule and await a coroutine on the specified loop

The coroutine is wrapped and scheduled using asyncio.run_coroutine_threadsafe(). While the coroutine is “awaited”, the result is not available as method returns immediately.

Parameters:
  • coro – The coroutine to schedule

  • loop – The event loop on which to schedule the coroutine

Note

This method is used internally by __call__() and is not meant to be called directly.

AioEventWaiters class

class AioEventWaiters[source]

Container used to manage await use with events

Used by pydispatch.dispatch.Event when it is awaited

waiters

Instances of AioEventWaiter currently “awaiting” the event

Type:

set

lock

A sync/async lock to guard modification to the waiters container during event emission

Type:

AioSimpleLock

Added in version 0.1.0.

__call__(*args: object, **kwargs: object) → None[source]

Triggers any stored waiters

Calls AioEventWaiter.trigger() method on all instances stored in waiters. After completion, the waiters are removed.

Parameters:
async add_waiter() → AioEventWaiter[source]

Add a AioEventWaiter to the waiters container

The event loop to use for AioEventWaiter.loop is found in the current context using asyncio.get_event_loop()

Returns:

The created AioEventWaiter instance

Return type:

waiter

async wait() → tuple[list[object] | tuple[object, ...], dict[str, object]][source]

Creates a waiter and “awaits” its result

This method is used by pydispatch.dispatch.Event instances when they are “awaited” and is the primary functionality of AioEventWaiters and AioEventWaiter.

Returns:

A two-tuple (args, kwargs) containing the positional and keyword arguments from the dispatched Event. args is a tuple, and kwargs is a dictionary. If the waiter’s event is set without triggering it, args remains the initial empty list and kwargs remains empty.

AioEventWaiter class

class AioEventWaiter(loop: AbstractEventLoop)[source]

Stores necessary information for a single “waiter”

Used by AioEventWaiters to handle awaiting an Event on a specific event loop

loop

The EventLoop instance

aio_event

An asyncio.Event used to track event emission

args

The positional arguments attached to the event. Initially an empty list; after a trigger, a tuple of arguments.

kwargs

The keyword arguments attached to the event

Type:

dict

Added in version 0.1.0.

trigger(*args: object, **kwargs: object) → None[source]

Called on event emission and notifies the wait() method

Called by AioEventWaiters when the Event instance is dispatched.

Positional and keyword arguments are stored as instance attributes for use in the wait() method and aio_event is set.

async wait() → tuple[list[object] | tuple[object, ...], dict[str, object]][source]

Waits for event emission and returns the event parameters

Returns:

A two-tuple (args, kwargs) containing the positional and keyword arguments from the dispatched Event. args is a tuple, and kwargs is a dictionary. If aio_event is set without calling trigger(), args remains the initial empty list and kwargs remains empty.

AioSimpleLock class

class AioSimpleLock[source]

asyncio.Lock alternative backed by a threading.Lock

This is a context manager that supports use in both with and async with context blocks.

lock

Instance of threading.Lock

Added in version 0.1.0.

acquire(blocking=True, timeout=-1)[source]

Acquire the lock

Parameters:
Returns:

True if the lock was acquired, otherwise False

Return type:

bool

async acquire_async()[source]

Acquire the lock asynchronously

release()[source]

Release the lock