How to set class attribute with await in __init__

Most magic methods aren’t designed to work with async def/await – in general, you should only be using await inside the dedicated asynchronous magic methods – __aiter__, __anext__, __aenter__, and __aexit__. Using it inside other magic methods either won’t work at all, as is the case with __init__ (unless you use some tricks described in … Read more

Using asyncio.Queue for producer-consumer flow

How can I modify the program above so that the producer(s) is its own coroutine that can be scheduled concurrently with the consumers/workers? The example can be generalized without changing its essential logic: Move the insertion loop to a separate producer coroutine. Start the consumers in the background, letting them process the items as they … Read more

What kind of problems (if any) would there be combining asyncio with multiprocessing?

You should be able to safely combine asyncio and multiprocessing without too much trouble, though you shouldn’t be using multiprocessing directly. The cardinal sin of asyncio (and any other event-loop based asynchronous framework) is blocking the event loop. If you try to use multiprocessing directly, any time you block to wait for a child process, … Read more

asyncio.ensure_future vs. BaseEventLoop.create_task vs. simple coroutine?

Actual info: Starting from Python 3.7 asyncio.create_task(coro) high-level function was added for this purpose. You should use it instead other ways of creating tasks from coroutimes. However if you need to create task from arbitrary awaitable, you should use asyncio.ensure_future(obj). Old info: ensure_future vs create_task ensure_future is a method to create Task from coroutine. It … Read more

Mocking async call in python 3.5

The solution was actually quite simple: I just needed to convert __call__ method of mock into coroutine: class AsyncMock(MagicMock): async def __call__(self, *args, **kwargs): return super(AsyncMock, self).__call__(*args, **kwargs) This works perfectly, when mock is called, code receives native coroutine Example usage: @mock.patch(‘my.path.asyncio.sleep’, new_callable=AsyncMock) def test_stuff(sleep): # code

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

Learning asyncio: “coroutine was never awaited” warning error

You made faire_toutes_les_requetes_sans_bloquer an awaitable function, a coroutine, by using async def. When you call an awaitable function, you create a new coroutine object. The code inside the function won’t run until you then await on the function or run it as a task: >>> async def foo(): … print(“Running the foo coroutine”) … >>> … 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