Event loop
- class mosaic.utils.event_loop.EventLoop(loop=None)[source]
Bases:
object
The event loop encapsulates the asyncio (or equivalent) event loop, which will run in a separate thread.
It provides helper functions to run things within the loop, in an executor, and to call functions after a period of time or every fixed amount of time.
- Parameters
loop (asyncio loop, optional) – Asyncio event loop to use internally, defaults to new loop.
- interval(coro, interval, *args, **kwargs)[source]
Run function every
interval
in seconds, starting afterinterval
seconds.- Parameters
coro (callable) – Function to execute in the loop.
interval (float) – Time to wait between executions in seconds.
args (tuple, optional) – Set of arguments for the function.
kwargs (optional) – Set of keyword arguments for the function.
- Returns
- Return type
concurrent.futures.Future
- run(coro, *args, **kwargs)[source]
Schedule a function in the event loop from synchronous code.
The call can be waited or returned immediately.
- Parameters
coro (callable) – Function to execute in the loop.
args (tuple, optional) – Set of arguments for the function.
kwargs (optional) – Set of keyword arguments for the function.
- Returns
- Return type
Return value from call or concurrent.futures.Future, depending on whether it is waited or not.
- run_in_executor(callback, *args, **kwargs)[source]
Run function in a thread executor.
- Parameters
callback (callable) – Function to execute.
args (tuple, optional) – Set of arguments for the function.
kwargs (optional) – Set of keyword arguments for the function.
- Returns
- Return type
asyncio.Future
- timeout(coro, timeout, *args, **kwargs)[source]
Run function after a certain
timeout
in seconds.- Parameters
coro (callable) – Function to execute in the loop.
timeout (float) – Time to wait before execution in seconds.
args (tuple, optional) – Set of arguments for the function.
kwargs (optional) – Set of keyword arguments for the function.
- Returns
- Return type
concurrent.futures.Future
- class mosaic.utils.event_loop.Future(name='anon', loop=None)[source]
Bases:
object
A local future associated with an EventLoop.
- Parameters
name (str, optional) – Name to give to the future, defaults to
anon
.loop (EventLoop, optional) – Loop associated with the future, defaults to current loop.
- property future
The wrapped future
- property state
Check the state of the future (
pending
,done
,cancelled
).
- property uid
Access the UID of the future.