Asychronous Utilities
minispeaker.asyncsync
poll_async_generator
def poll_async_generator(
stream: AsyncGenerator[T, S],
default_empty_factory: Callable[[], E] = lambda: None,
loop: Optional[AbstractEventLoop] = None) -> Generator[T | E, S, None]
Converts a asynchronous generator stream
into a synchronous one via polling.
Arguments:
stream
AsyncGenerator[T, S] - AnyAsyncgenerator
.default_empty_factory
Callable[[], E], optional - A function whose return value is used when the next polled data fromstream
is not available. Defaults to lambda:None.loop
Optional[AbstractEventLoop], optional - The event loop containingstream
. Defaults to `get_event_loop()``
Returns:
Generator[T | E, S, None]: A stream
equivalent synchronous generator
Yields:
T | E: When no stream data is available, return E
. Otherwise fetch T
.
Event
class Event()
Class implementing event objects, that will dynamically determines which method(asynchronous/synchronous) to wait.
Events manage a flag that can be set to true with the set() method and reset to false with the clear() method. The wait() method blocks until the flag is true. The flag is initially false.
Warnings:
This class will automatically determine when to call a asynchronous wait
or synchronous wait
. To directly use the synchronous wait
, use Event.tevent
like Threading.Event
Event.clear
def clear()
Reset the internal flag to false.
Subsequently, coroutines and threads calling wait() will block until set() is called to set the internal flag to true again.
Event.set
def set()
Set the internal flag to true. All threads and coroutines waiting for it to become true are awakened.
Coroutines and Threads that call wait() once the flag is true will not block at all.
Event.wait
def wait(
timeout: Optional[float] = None
) -> bool | Coroutine[Any, Any, Literal[True]]
Dynamically determines which method(asynchronous/synchronous) to wait.
Arguments:
timeout
Optional[float] - A floating point number specifying a timeout for the operation in seconds (or fractions thereof) to block. Defaults to None.
Returns:
bool | Coroutine[Any, Any, Literal[True]]: If no asynchronous loop is present, wait identical to threading.Event().wait(). Otherwise, return an equivalent coroutine of Event().wait().
Event.is_set
def is_set() -> bool
Return True if and only if the internal flag is true.