multiprocessing vs multithreading vs asyncio

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

How to do multiprocessing in FastAPI

async def endpoint You could use loop.run_in_executor with ProcessPoolExecutor to start function at a separate process. @app.post(“/async-endpoint”) async def test_endpoint(): loop = asyncio.get_event_loop() with concurrent.futures.ProcessPoolExecutor() as pool: result = await loop.run_in_executor(pool, cpu_bound_func) # wait result def endpoint Since def endpoints are run implicitly in a separate thread, you can use the full power of modules … Read more

Can an asyncio event loop run in the background without suspending the Python interpreter?

Edit: If using Python 3.8 or above, you should use the asyncio repl, as explained in zeronone’s answer. If using 3.7 or lower, you can use this answer. You can run the event loop inside a background thread: >>> import asyncio >>> >>> @asyncio.coroutine … def greet_every_two_seconds(): … while True: … print(‘Hello World’) … yield … Read more

Python simple socket client/server using asyncio

The closest literal translation of the threading code would create the socket as before, make it non-blocking, and use asyncio low-level socket operations to implement the server. Here is an example, sticking to the more relevant server part (the client is single-threaded and likely fine as-is): import asyncio, socket async def handle_client(client): loop = asyncio.get_event_loop() … Read more

Simplest async/await example possible in Python

To answer your questions, I will provide 3 different solutions to the same problem. Case 1: just normal Python import time def sleep(): print(f’Time: {time.time() – start:.2f}’) time.sleep(1) def sum(name, numbers): total = 0 for number in numbers: print(f’Task {name}: Computing {total}+{number}’) sleep() total += number print(f’Task {name}: Sum = {total}\n’) start = time.time() tasks … Read more

How to 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

asyncio.sleep() vs time.sleep() in Python

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

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