asyncio run or run_until_complete

Is there a simple way to make [code making use of] backwards compatible with Python 3.6?

You can implement a simple substitute for and call it on older Python versions:

import asyncio, sys, types

def run(coro):
    if sys.version_info >= (3, 7):

    # Emulate on older versions

    # requires a coroutine, so require it here as well
    if not isinstance(coro, types.CoroutineType):
        raise TypeError("run() requires a coroutine object")

    loop = asyncio.new_event_loop()
        return loop.run_until_complete(coro)

The advantage of this approach over just using loop.run_until_complete() is that you’re executing your code under the semantics close to those of the new, even on older Python versions. (For example, you will always run on a freshly created event loop.) Dropping support for pre-3.7 Python will be as easy as removing the run shim and calling directly.

Leave a Comment