utilities
asyncio
marvin.utilities.asyncio
Constants
T
Functions
run_sync
Run a coroutine synchronously.
This function uses asyncio to run a coroutine in a synchronous context. It attempts the following strategies in order:
- If no event loop is running, creates a new one and runs the coroutine
- If a loop is running, attempts to run the coroutine on that loop
- As a last resort, creates a new thread with its own event loop to run the coroutine
Context variables are properly propagated between threads in all cases.
Example:
Args: coro: The coroutine to run synchronously
Returns: The result of the coroutine
run_sync_in_thread
Run a coroutine synchronously in a new thread.
This function creates a new thread with its own event loop to run the coroutine. Context variables are properly propagated between threads. This is useful when you need to run async code in a context where you can’t use the current event loop (e.g., inside an async frame).
Example:
Args: coro: The coroutine to run synchronously
Returns: The result of the coroutine
Parent Module: utilities