Combine awaitables like Promise.all

The equivalent would be using asyncio.gather: import asyncio async def bar(i): print(‘started’, i) await asyncio.sleep(1) print(‘finished’, i) async def main(): await asyncio.gather(*[bar(i) for i in range(10)]) loop = asyncio.get_event_loop() loop.run_until_complete(main()) loop.close() Why doesn’t my approach work? Because when you await each item in seq, you block that coroutine. So in essence, you have synchronous code … 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

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

Sharing python objects across multiple workers

It is not possible to share a python object between different processes straightforwardly. The facilities included in the multiprocessing module (like managers or shared memory) are not suitable for sharing resources between workers, since they require a master process creating the resources and do not have the durability property. Also server processes can be run … Read more

When using asyncio, how do you allow all running tasks to finish before shutting down the event loop

You can retrieve unfinished tasks and run the loop again until they finished, then close the loop or exit your program. pending = asyncio.all_tasks() loop.run_until_complete(asyncio.gather(*pending)) pending is a list of pending tasks. asyncio.gather() allows to wait on several tasks at once. If you want to ensure all the tasks are completed inside a coroutine (maybe … 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

How can I send an HTTP request from my FastAPI app to another site (API)?

requests is a synchronous library. You need to use an asyncio-based library to make requests asynchronously. httpx httpx is typically used in FastAPI applications to request external services. It provides synchronous and asynchronous clients which can be used in def and async def path operations appropriately. It is also recommended for asynchronous tests of application. … Read more