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.

get_event_loop()[source]

Access the internal loop.

Return type:

asyncio loop

interval(coro, interval, *args, **kwargs)[source]

Run function every interval in seconds, starting after interval 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.

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.

Return type:

Return value from call or concurrent.futures.Future, depending on whether it is waited or not.

run_forever()[source]

Run event loop forever.

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.

Return type:

asyncio.Future

set_main_thread()[source]

Set loop thread as main thread.

stop()[source]

Stop the event loop.

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.

Return type:

concurrent.futures.Future

wrap_future(future)[source]

Wrap a concurrent.futures.Future to be compatible with asyncio.

Parameters:

future (concurrent.futures.Future) –

Return type:

asyncio.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.

add_done_callback(fun)[source]

Add done callback.

Parameters:

fun (callable) –

cancelled()[source]

Check whether the future is cancelled.

done()[source]

Check whether the future is done.

exception()[source]

Get the future exception.

property future

The wrapped future

result()[source]

Get the future result.

set_exception(exc)[source]

Set the future exception.

Parameters:

exc (Exception) –

set_result(result)[source]

Set the future result.

Parameters:

result (object) –

property state

Check the state of the future (pending, done, cancelled).

property uid

Access the UID of the future.

class mosaic.utils.event_loop.async_property(func, field_name=None)[source]

Bases: object

awaitable_only(instance)[source]
get_loader(instance)[source]
mosaic.utils.event_loop.gather(tasks)[source]

Wait for the termination of a group of tasks concurrently.

Parameters:

tasks (list) – Set of tasks to wait.

Returns:

Set of results from the task list.

Return type:

list