euporie.core.async_utils
Shared asyncio event loop management for Euporie subsystems.
Functions
|
Get or create an event loop for a given subsystem name. |
|
Schedule a coroutine on a loop from any context, returning a Future. |
|
Run a coroutine synchronously from a sync context. |
- euporie.core.async_utils.get_or_create_loop(name: str) AbstractEventLoop
Get or create an event loop for a given subsystem name.
In Pyodide, always returns the main event loop without spawning threads. In normal Python, creates one background thread per unique name.
- Parameters:
name – The subsystem name (e.g., “kernel”, “convert”, “lsp”)
- Returns:
An asyncio event loop for the named subsystem
- euporie.core.async_utils.run_coro_async(coro: Coroutine[Any, Any, T], loop: asyncio.AbstractEventLoop, *, cancel_previous: bool = False, previous_tasks: dict[str, asyncio.Future] | None = None, callback: Callable[[T], None] | None = None) asyncio.Future[T]
Schedule a coroutine on a loop from any context, returning a Future.
Handles both normal Python (background thread loop) and Pyodide (main loop) scheduling transparently.
- Parameters:
coro – The coroutine to schedule
loop – The event loop to run on
cancel_previous – If True, cancel any previous task for this coroutine
previous_tasks – Dict to track and optionally cancel previous tasks by name
callback – Optional callback to run when the coroutine completes
- Returns:
A Future that will contain the result
- euporie.core.async_utils.run_coro_sync(coro: Coroutine[Any, Any, T], loop: asyncio.AbstractEventLoop | None = None) T
Run a coroutine synchronously from a sync context.
In Pyodide, where the main loop is already running, this yields control back to the event loop repeatedly until the coroutine completes.
In normal Python with a background loop, this uses run_coroutine_threadsafe.
- Parameters:
coro – The coroutine to run
loop – The event loop to run on (if None, uses current running loop or raises)
- Returns:
The result of the coroutine
- Raises:
RuntimeError – If no loop is provided and none is running