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 per process. Device I/O and
coroutines connected to psygnal signals run there, off the emitting GUI
thread.
run_coro is for general use: synchronous code, such as a presenter method or
a Qt slot, runs a coroutine on the loop with it and gets the result. The rest
of the module is set up by the container at startup and torn down at shutdown.
Components must not build their own loop or install a backend.
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
|
Return the |
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 container sets these up at startup and tears them down at shutdown.
They are documented so the runtime can be inspected, not for components to
call: a second backend, or a loop beside the shared one, breaks signal
dispatch for the whole process. Reach the shared loop with
run_coro.
Return the background event loop, starting it on its own thread on first use.
Source code in src/redsun/aio.py
Install the culsans backend as psygnal's async backend.
Call it before connecting a coroutine to a signal. A second call returns the
installed backend; tear it down with psygnal's 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 run as tasks on the loop from get_shared_loop, so signals
emitted on any thread are delivered.
Source code in src/redsun/aio.py
running
property
¶
Return the event set while 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 the event can be set and cleared from any thread
and awaited from a coroutine.