Skip to content

_utils

Utils

This module contains utility functions.

Functions

  • get_loop: Get event loop.

get_loop()

Get event loop.

Returns:

Type Description
AbstractEventLoop

asyncio.AbstractEventLoop: The event loop.

Examples:

>>> get_loop()
<_UnixSelectorEventLoop running=True closed=False debug=False>
Source code in src/bit24/_utils.py
def get_loop() -> asyncio.AbstractEventLoop:
    """
    Get event loop.

    Returns:
        asyncio.AbstractEventLoop: The event loop.

    Examples:
        >>> get_loop()
        <_UnixSelectorEventLoop running=True closed=False debug=False>
    """
    try:
        return asyncio.get_event_loop()
    except RuntimeError as e:
        if str(e).startswith("There is no current event loop in thread"):
            loop = asyncio.new_event_loop()
            asyncio.set_event_loop(loop)
            return loop
        raise