64

I have a few blocking functions foo, bar and I can't change those (Some internal library I don't control. Talks to one or more network services). How do I use it as async? E.g. I want to do the following:

results = []
for inp in inps:
    val = foo(inp)
    result = bar(val)
    results.append(result)

This will be inefficient as I can call foo for the second input while I am waiting for the first and same for bar. How do I wrap them such that they are usable with asyncio (i.e new async, await syntax)?

Lets assume the functions are re-entrant. i.e it is fine to call foo again when already a previous foo is processing.


Update

Extending answer with reusable decorator. Click here for example.

def run_in_executor(f):
    @functools.wraps(f)
    def inner(*args, **kwargs):
        loop = asyncio.get_running_loop()
        return loop.run_in_executor(None, functools.partial(f, *args, **kwargs))

    return inner
1
  • 6
    stackoverflow documentation has been shutdown
    – ospider
    May 25, 2018 at 1:53

4 Answers 4

61

There are (sort of) two questions here:

  1. how can I run blocking code asynchronously within a coroutine
  2. how can I run multiple async tasks at the "same" time (as an aside: asyncio is single-threaded, so it is concurrent, but not truly parallel).

Concurrent tasks can be created using the high-level asyncio.create_task or the low-level asyncio.ensure_future. Starting with 3.11, they can also be created through asyncio task groups, as pioneered by the Trio library (the creator of Trio has an excellent blog post on the subject here).

To run synchronous code, you will need to run the blocking code in an executor. Example:

import concurrent.futures
import asyncio
import time

def blocking(delay):
    time.sleep(delay)
    print('Completed.')


async def non_blocking(executor):
    loop = asyncio.get_running_loop()
    # Run three of the blocking tasks concurrently. asyncio.wait will
    # automatically wrap these in Tasks. If you want explicit access
    # to the tasks themselves, use asyncio.ensure_future, or add a
    # "done, pending = asyncio.wait..." assignment
    await asyncio.wait(
        fs={
            # Returns after delay=12 seconds
            loop.run_in_executor(executor, blocking, 12),
            
            # Returns after delay=14 seconds
            loop.run_in_executor(executor, blocking, 14),
            
            # Returns after delay=16 seconds
            loop.run_in_executor(executor, blocking, 16)
        },
        return_when=asyncio.ALL_COMPLETED
    )

executor = concurrent.futures.ThreadPoolExecutor(max_workers=5)
asyncio.run(non_blocking(executor))

If you want to schedule these tasks using a for loop (as in your example), you have several different strategies, but the underlying approach is to schedule the tasks using the for loop (or list comprehension, etc), await them with asyncio.wait, and then retrieve the results. Example:

done, pending = await asyncio.wait(
    fs=[loop.run_in_executor(executor, blocking_foo, *args) for args in inps],
    return_when=asyncio.ALL_COMPLETED
)

# Note that any errors raise during the above will be raised here; to
# handle errors you will need to call task.exception() and check if it
# is not None before calling task.result()
results = [task.result() for task in done]
6
  • asyncio.ensure_future is not mandatory in this case as asyncio.wait will already wrap coroutines in Tasks.
    – lovebug356
    Dec 12, 2016 at 10:35
  • Good tip! Though for a less-toy example, ensure_future might still be useful for ex. cancellation (irrespective of pending, complete). Dec 19, 2016 at 21:46
  • 1
    The anchor in the link to "run the blocking code in an executor" is out of date -- this one seem to be the latest at this point.
    – gpano
    Nov 12, 2019 at 4:49
  • So this will not really be in parallel ? How can I turn into really parallel code? why asyncio.sleep(). is truly parallel ? Nov 23, 2020 at 9:35
  • @user1628688 it depends if you really mean parallel, or if you actually mean concurrent. See here for an explanation of the difference. (side note, I had to correct my original answer, as I had them reversed there). Most cpython code cannot be parallel, and must be only concurrent, because of the GIL. This is a language runtime limitation; it's not something you can avoid without getting low-level. Furthermore, async/await code is (AFAIK) inherently single-threaded anyways. Dec 13, 2020 at 7:10
36

Extending the accepted answer to actually solve the problem in question.

Note: Requires python 3.7+

import functools

from urllib.request import urlopen
import asyncio


def legacy_blocking_function():  # You cannot change this function
    r = urlopen("https://example.com")
    return r.read().decode()


def run_in_executor(f):
    @functools.wraps(f)
    def inner(*args, **kwargs):
        loop = asyncio.get_running_loop()
        return loop.run_in_executor(None, lambda: f(*args, **kwargs))

    return inner


@run_in_executor
def foo(arg):  # Your wrapper for async use
    resp = legacy_blocking_function()
    return f"{arg}{len(resp)}"


@run_in_executor
def bar(arg):  # Another wrapper
    resp = legacy_blocking_function()
    return f"{len(resp)}{arg}"


async def process_input(inp):  # Modern async function (coroutine)
    res = await foo(inp)
    res = f"XXX{res}XXX"
    return await bar(res)


async def main():
    inputs = ["one", "two", "three"]
    input_tasks = [asyncio.create_task(process_input(inp)) for inp in inputs]
    print([await t for t in asyncio.as_completed(input_tasks)])
    # This doesn't work as expected :(
    # print([await t for t in asyncio.as_completed([process_input(inp) for inp in input_tasks])])


if __name__ == '__main__':
    asyncio.run(main())

Click here for up to date version of this example and to send pull requests.

1
  • 5
    I ran across this one when was looking for an answer. This example is very informative, but not fully correct. Actual wrapper function should contain async/await keywords. def run_in_executor(f): @functools.wraps(f) async def inner(*args, **kwargs): loop = asyncio.get_running_loop() return await loop.run_in_executor(None, lambda: f(*args, **kwargs)) return inner
    – Nikita
    Apr 1, 2021 at 14:45
8

Now you can also just use asyncio.to_thread to asynchronously run function in a separate thread.

import asyncio
import time

def blocking():
    print("Started!")
    time.sleep(5)
    print("Finished!")

async def non_blocking():
    print("Started!")
    await asyncio.sleep(5)
    print("Finished!")

async def main():
    print("Blocking function:")
    start_time = time.time()
    await asyncio.gather(*(asyncio.to_thread(blocking) for i in range(5)))
    print("--- %s seconds ---" % (time.time() - start_time))

    print("Non-blocking function:")
    start_time = time.time()
    await asyncio.gather(*(non_blocking() for i in range(5)))
    print("--- %s seconds ---" % (time.time() - start_time))

asyncio.run(main())

Output:

Blocking function:
Started!
Started!
Started!
Started!
Started!
Finished!
Finished!
Finished!
Finished!
Finished!
--- 5.018239736557007 seconds ---
Non-blocking function:
Started!
Started!
Started!
Started!
Started!
Finished!
Finished!
Finished!
Finished!
Finished!
--- 5.005802392959595 seconds ---

Documentation: https://docs.python.org/3.11/library/asyncio-task.html#asyncio.to_thread

2
  • 2
    Note: This function is available from python 3.9
    – balki
    Sep 14, 2022 at 14:33
  • How do I use a semaphore with this ? Feb 11 at 3:00
5
import asyncio
from time import sleep
import logging

logging.basicConfig(
    level=logging.DEBUG, format="%(asctime)s %(thread)s %(funcName)s %(message)s")


def long_task(t):
    """Simulate long IO bound task."""
    logging.info("2. t: %s", t)
    sleep(t)
    logging.info("4. t: %s", t)
    return t ** 2


async def main():
    loop = asyncio.get_running_loop()
    inputs = range(1, 5)
    logging.info("1.")
    futures = [loop.run_in_executor(None, long_task, i) for i in inputs]
    logging.info("3.")
    results = await asyncio.gather(*futures)
    logging.info("5.")
    for (i, result) in zip(inputs, results):
        logging.info("6. Result: %s, %s", i, result)


if __name__ == "__main__":
    asyncio.run(main())

Output:

2020-03-18 17:13:07,523 23964 main 1.
2020-03-18 17:13:07,524 5008 long_task 2. t: 1
2020-03-18 17:13:07,525 21232 long_task 2. t: 2
2020-03-18 17:13:07,525 22048 long_task 2. t: 3
2020-03-18 17:13:07,526 25588 long_task 2. t: 4
2020-03-18 17:13:07,526 23964 main 3.
2020-03-18 17:13:08,526 5008 long_task 4. t: 1
2020-03-18 17:13:09,526 21232 long_task 4. t: 2
2020-03-18 17:13:10,527 22048 long_task 4. t: 3
2020-03-18 17:13:11,527 25588 long_task 4. t: 4
2020-03-18 17:13:11,527 23964 main 5.
2020-03-18 17:13:11,528 23964 main 6. Result: 1, 1
2020-03-18 17:13:11,528 23964 main 6. Result: 2, 4
2020-03-18 17:13:11,529 23964 main 6. Result: 3, 9
2020-03-18 17:13:11,529 23964 main 6. Result: 4, 16

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.