Utils Reference

aio_trader.utils.configure_default_logger(name='aio_trader') Logger

Return a configured logger

Parameters:

name (str) – Name of logger instance to be returned. Default aio_trader

aio_trader.utils.add_signal_handlers(handler: Callable, *args: Any, **kwargs: Any) None

Add signal handlers for KeyboardInterrupt and terminate/kill commands.

Any positional or keyword arguments are passed to handler

Parameters:
  • handler (Callable) – Async function to perform clean up operations before shutdown.

  • args (Any) – Positional arguments to pass to handler function

  • kwargs (Any) – Keyword arguments to pass to handler function

See example Handling Keyboard Interrupt for usage

async aio_trader.utils.as_completed(tasks: List[Task]) AsyncGenerator[Task, None]

Similar to asyncio.as_completed but returns the original Task object.

Source: https://stackoverflow.com/a/55531688

Parameters:

tasks (List[asyncio.Task]) – A list of Task objects

Returns:

AsyncGenerator[asyncio.Task, None]

When calling asyncio.as_completed, results are not returned in insertion order, but in the order of completion.

The result is a Coroutine object and not the original Task object. There is no link to the original Task, so any context information like the Task name is lost forever.

Code Explanation:

Every task must be assigned a name, providing some context information.

task = asyncio.create_task(fn, name='my_task')
task.get_name() # my_task

This function creates a Futures object and adds the Futures.set_result method as a callback to the task.

The futures are added to a list and passed to asyncio.as_completed awaiting completion.

When the task is completed, the callback is called. It receives the completed task object and marks the Futures object as done.

Usage:

# tasks: List[asyncio.Task]

async for task in as_completed(tasks):
    name = task.get_name()
    result = task.result()

Read the in-code documentation for further understanding.

See example Downloading historical data for usage