blatann.waitables.waitable module

class blatann.waitables.waitable.Waitable(n_args=1)

Bases: Generic[T]

Base class for an object which can be waited on for an operation to complete. This is a similar concept to concurrent.futures.Future where asynchronous operations can block the current thread, or register a handler to be called when it completes.

wait(timeout=None, exception_on_timeout=True)

Waits for the asynchronous operation to complete

Warning

If this call times out, it cannot be (successfully) called again as it will clean up all event handlers for the waitable. This is done to remove lingering references to the waitable object through event subscriptions

Parameters:
  • timeout (Optional[float]) – How long to wait, or None to wait indefinitely

  • exception_on_timeout – Flag to either throw an exception on timeout, or instead return None object(s)

Return type:

Optional[TypeVar(T)]

Returns:

The result of the asynchronous operation

Raises:

TimeoutError

async as_async(timeout=None, exception_on_timeout=True, loop=None)

Waits for the asynchronous operation to complete that can be ``await``ed in async methods.

Warning

This method is experimental!

Parameters:
  • timeout (Optional[float]) – How long to wait, or None to wait indefinitely

  • exception_on_timeout – Flag to either throw an exception on timeout, or instead return None object(s)

  • loop (Optional[AbstractEventLoop]) – Optional asyncio event loop to use instead of the default one returned by asyncio.get_event_loop()

Return type:

Optional[TypeVar(T)]

Returns:

The result of the asynchronous operation

Raises:

TimeoutError

then(callback)

Registers a function callback that will be called when the asynchronous operation completes

Note

Only a single callback is supported– subsequent calls to this method will overwrite previous callbacks

Parameters:

callback (Callable[[TypeVar(T)], None]) – The function to call when the async operation completes

Returns:

This waitable object

class blatann.waitables.waitable.GenericWaitable(n_args=1)

Bases: Waitable[T]

Simple wrapper of a Waitable object which exposes a notify method so external objects can signal/trigger the waitable’s response

notify(*results)
class blatann.waitables.waitable.EmptyWaitable(*args)

Bases: Waitable[T]

Waitable class which will immediately return the args provided when waited on or when a callback function is registered

wait(timeout=None, exception_on_timeout=True)

Waits for the asynchronous operation to complete

Warning

If this call times out, it cannot be (successfully) called again as it will clean up all event handlers for the waitable. This is done to remove lingering references to the waitable object through event subscriptions

Parameters:
  • timeout (Optional[float]) – How long to wait, or None to wait indefinitely

  • exception_on_timeout – Flag to either throw an exception on timeout, or instead return None object(s)

Return type:

TypeVar(T)

Returns:

The result of the asynchronous operation

Raises:

TimeoutError

then(callback)

Registers a function callback that will be called when the asynchronous operation completes

Note

Only a single callback is supported– subsequent calls to this method will overwrite previous callbacks

Parameters:

callback (Callable[[TypeVar(T)], None]) – The function to call when the async operation completes

Returns:

This waitable object