Asyncio.gather vs asyncio.wait

Although similar in general cases (“run and get results for many tasks”), each function has some specific functionality for other cases: asyncio.gather() Returns a Future instance, allowing high level grouping of tasks: import asyncio from pprint import pprint import random async def coro(tag): print(“>”, tag) await asyncio.sleep(random.uniform(1, 3)) print(“<“, tag) return tag loop = asyncio.get_event_loop() … Read more

asyncio CancelledError and KeyboardInterrupt

task.cancel() itself doesn’t finish the task: it just says to task that CancelledError should be raised inside it and returns immediately. You should call it and await while task would be actually cancelled (while it’ll raise CancelledError). You also shouldn’t suppress CancelledError inside task. Read this answer where I tried to show different ways of … Read more

How to use asyncio with existing blocking library?

There are (sort of) two questions here: first, how to run blocking code asynchronously, and second, how to run async code in parallel (asyncio is single-threaded, so the GIL still applies, so it isn’t truly parallel, but I digress). Concurrent tasks can be created using asyncio.ensure_future, as documented here. To run synchronous code, you will … Read more

How can I call an async function without await?

One way would be to use create_task function: import asyncio async def handler_message(request): … loop = asyncio.get_event_loop() loop.create_task(perform_message(x,y,z)) … As per the loop documentation, starting Python 3.10, asyncio.get_event_loop() is deprecated. If you’re trying to get a loop instance from a coroutine/callback, you should use asyncio.get_running_loop() instead. This method will not work if called from the … Read more

How to combine python asyncio with threads?

It’s pretty simple to delegate a method to a thread or sub-process using BaseEventLoop.run_in_executor: import asyncio import time from concurrent.futures import ProcessPoolExecutor def cpu_bound_operation(x): time.sleep(x) # This is some operation that is CPU-bound @asyncio.coroutine def main(): # Run cpu_bound_operation in the ProcessPoolExecutor # This will make your coroutine block, but won’t block # the event … Read more

multiprocessing vs multithreading vs asyncio in Python 3

TL;DR Making the Right Choice: We have walked through the most popular forms of concurrency. But the question remains – when should choose which one? It really depends on the use cases. From my experience (and reading), I tend to follow this pseudo code: if io_bound: if io_very_slow: print(“Use Asyncio”) else: print(“Use Threads”) else: print(“Multi … Read more

asyncio.sleep() vs time.sleep()

You aren’t seeing anything special because there’s nothing much asynchronous work in your code. However, the main difference is that time.sleep(5) is blocking, and asyncio.sleep(5) is non-blocking. When time.sleep(5) is called, it will block the entire execution of the script and it will be put on hold, just frozen, doing nothing. But when you call … Read more

FastAPI runs api-calls in serial instead of parallel fashion

As per FastAPI’s documentation: When you declare a path operation function with normal def instead of async def, it is run in an external threadpool that is then awaited, instead of being called directly (as it would block the server). Thus, def (sync) routes run in a separate thread from a threadpool, or, in other … Read more

Python 3.7 – asyncio.sleep() and time.sleep()

You aren’t seeing anything special because there’s nothing much asynchronous work in your code. However, the main difference is that time.sleep(5) is blocking, and asyncio.sleep(5) is non-blocking. When time.sleep(5) is called, it will block the entire execution of the script and it will be put on hold, just frozen, doing nothing. But when you call … Read more