aea.helpers.async_
utils
This module contains the misc utils for async code.
ensure_
list
Return [value] or list(value) if value is a sequence.
AsyncState Objects
Awaitable state.
__
init__
Init async state.
Arguments:
initial_state
: state to set on start.states_enum
: container of valid states if not provided state not checked on set.
set
Set state.
add_
callback
Add callback to track state changes.
Arguments:
callback_fn
: callable object to be called on state changed.
get
Get state.
wait
Wait state to be set.
Arguments:
state_or_states
: state or list of states.
Returns:
tuple of previous state and new state.
transit
| @contextmanager
| transit(initial: Any = not_set, success: Any = not_set, fail: Any = not_set) -> Generator
Change state context according to success or not.
Arguments:
initial
: set state on context enter, not_set by defaultsuccess
: set state on context block done, not_set by defaultfail
: set state on context block raises exception, not_set by default :yield: generator
PeriodicCaller Objects
Schedule a periodic call of callable using event loop.
Used for periodic function run using asyncio.
__
init__
| __init__(callback: Callable, period: float, start_at: Optional[datetime.datetime] = None, exception_callback: Optional[Callable[[Callable, Exception], None]] = None, loop: Optional[AbstractEventLoop] = None) -> None
Init periodic caller.
Arguments:
callback
: function to call periodicallyperiod
: period in seconds.start_at
: optional first call datetimeexception_callback
: optional handler to call on exception raised.loop
: optional asyncio event loop
start
Activate period calls.
stop
Remove from schedule.
AnotherThreadTask Objects
Schedule a task to run on the loop in another thread.
Provides better cancel behaviour: on cancel it will wait till cancelled completely.
__
init__
Init the task.
Arguments:
coro
: coroutine to scheduleloop
: an event loop to schedule on.
result
Wait for coroutine execution result.
Arguments:
timeout
: optional timeout to wait in seconds.
Returns:
result
cancel
Cancel coroutine task execution in a target loop.
done
Check task is done.
ThreadedAsyncRunner Objects
Util to run thread with event loop and execute coroutines inside.
__
init__
Init threaded runner.
Arguments:
loop
: optional event loop. is it's running loop, threaded runner will use it.
start
Start event loop in dedicated thread.
run
Run code inside thread.
call
Run a coroutine inside the event loop.
Arguments:
coro
: a coroutine to run.
Returns:
task
stop
Stop event loop in thread.
Runnable Objects
Abstract Runnable class.
Use to run async task in same event loop or in dedicated thread. Provides: start, stop sync methods to start and stop task Use wait_completed to await task was completed.
__
init__
Init runnable.
Arguments:
loop
: asyncio event loop to use.threaded
: bool. start in thread if True.
start
Start runnable.
Returns:
bool started or not.
is_
running
Get running state.
run
Implement run logic respectful to CancelError on termination.
wait_
completed
| wait_completed(sync: bool = False, timeout: float = None, force_result: bool = False) -> Awaitable
Wait runnable execution completed.
Arguments:
sync
: bool. blocking waittimeout
: float secondsforce_result
: check result even it was waited.
Returns:
awaitable if sync is False, otherwise None
stop
Stop runnable.
start_
and_
wait_
completed
Alias for start and wait methods.