Skip to content

mode.loop

AsyncIO event loop implementations.

This contains a registry of different AsyncIO loop implementations to be used with Mode.

The choices available are:

aio default Normal asyncio event loop policy.

eventlet

Use eventlet as the event loop.

This uses aioeventlet and will apply the eventlet monkey-patches.

To enable execute the following as the first thing that happens when your program starts (e.g. add it as the top import of your entrypoint module):

import mode.loop
mode.loop.use('eventlet')

gevent deprecated, currently broken

Warning

This backend is unmaintained, has no test coverage, and does not presently work on any interpreter: selecting it raises ImportError: Cannot import 'Loop' from mode.loop._gevent_loop with current gevent releases, on GIL-enabled and free-threaded builds alike. It also re-enables the GIL on free-threaded builds, because gevent.libev.corecext does not declare that it is safe without it.

Selecting it raises a DeprecationWarning, and it will be removed in a future major release. Use aio (the default) or uvloop.

Use gevent as the event loop.

This uses aiogevent (+modifications) and will apply the gevent monkey-patches.

This choice enables you to run blocking Python code as if they have invisible async/await syntax around it (NOTE: C extensions are not usually gevent compatible).

To enable execute the following as the first thing that happens when your program starts (e.g. add it as the top import of your entrypoint module):

import mode.loop
mode.loop.use('gevent')

uvloop

Event loop using uvloop.

To enable execute the following as the first thing that happens when your program starts (e.g. add it as the top import of your entrypoint module):

import mode.loop
mode.loop.use('uvloop')

use(loop)

Specify the event loop to use as a string.

Loop must be one of: aio, eventlet, gevent, uvloop.

Note

gevent is deprecated and currently broken -- selecting it raises a DeprecationWarning and then fails to import. See the module docstring.

Source code in mode/loop/__init__.py
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
def use(loop: str) -> None:
    """Specify the event loop to use as a string.

    Loop must be one of: aio, eventlet, gevent, uvloop.

    Note:
        `gevent` is deprecated and currently broken -- selecting it raises
        a `DeprecationWarning` and then fails to import.  See the module
        docstring.
    """
    deprecated = DEPRECATED_LOOPS.get(loop)
    if deprecated is not None:
        # stacklevel=2 attributes this to the caller, so it is actually
        # shown when selected from an entrypoint module.
        warnings.warn(deprecated, DeprecationWarning, stacklevel=2)
    mod = LOOPS.get(loop, loop)
    if mod is not None:
        importlib.import_module(mod)