Async runtime¶
Design rationale: ADR 0005.
Running coroutines¶
Shared background event loop and dispatch of coroutines connected to signals.
Redsun runs one background asyncio event loop for the whole process. Device
I/O and any coroutine connected to a psygnal signal execute there, off the GUI
thread that emits.
Only run_coro is meant for general use: it is how synchronous code - a
presenter method, a Qt slot - runs a coroutine on that loop and gets its
result. Everything else in this module is application plumbing, set up by the
application container during startup and torn down on shutdown. Components
should not build a loop or install a backend of their own.
run_coro
¶
Run a coroutine in the background event loop and return its result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coro
|
Coroutine
|
The coroutine to run. |
required |
return_future
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
R
|
The result of the coroutine. |
Source code in src/redsun/aio.py
Internal machinery¶
Not part of the public API
The symbols below are wired up by the application container at startup and
torn down on shutdown. They are documented so that the runtime's behaviour
is inspectable, not so that components call them: installing a second
backend, or building a loop alongside the shared one, breaks signal
dispatch for the whole process. Use run_coro to
reach the shared loop.
Return the background event loop.
Returns:
| Type | Description |
|---|---|
AbstractEventLoop
|
The shared event loop. |
Install the culsans backend as psygnal's active async backend.
Must be called before connecting a coroutine to a signal. Calling it
again returns the backend installed by the first call; tear it down with
psygnal's own clear_async_backend.
Returns:
| Type | Description |
|---|---|
CulsansAsyncioBackend
|
The active backend. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If a different async backend is already active. |
Source code in src/redsun/aio.py
Bases: _AsyncBackend, Loggable
Psygnal async backend draining a culsans queue on the shared loop.
Queued callbacks are dispatched as tasks on the loop returned by
get_shared_loop, so signals emitted from any thread are delivered.
Source code in src/redsun/aio.py
running
property
¶
Return the event indicating whether the backend accepts callbacks.
put
¶
close
¶
run
async
¶
Drain the queue until it is shut down or the drain is cancelled.
Source code in src/redsun/aio.py
Resettable event whose wait is a coroutine.
Wraps aiologic.REvent so that the event can be set and cleared from any
thread while still being awaited from a coroutine.