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

@asyncio.coroutine vs async def

Yes, there are functional differences between native coroutines using async def syntax and generator-based coroutines using the asyncio.coroutine decorator. According to PEP 492, which introduces the async def syntax: Native coroutine objects do not implement __iter__ and __next__ methods. Therefore, they cannot be iterated over or passed to iter(), list(), tuple() and other built-ins. They … Read more

What is the difference between concurrent.futures and asyncio.futures?

The asyncio documentation covers the differences: class asyncio.Future(*, loop=None) This class is almost compatible with concurrent.futures.Future. Differences: result() and exception() do not take a timeout argument and raise an exception when the future isn’t done yet. Callbacks registered with add_done_callback() are always called via the event loop’s call_soon_threadsafe(). This class is not compatible with the … Read more

what’s Python asyncio.Lock() for?

You use it for the same reason you’d use a lock in threaded code: to protect a critical section. asyncio is primarily meant for use in single-threaded code, but there is still concurrent execution happening (any time you hit a yield from or await), which means sometimes you need synchronization. For example, consider a function … Read more

When to use and when not to use Python 3.5 `await` ?

By default all your code is synchronous. You can make it asynchronous defining functions with async def and “calling” these functions with await. A More correct question would be “When should I write asynchronous code instead of synchronous?”. Answer is “When you can benefit from it”. In cases when you work with I/O operations as … Read more

Please explain “Task was destroyed but it is pending!” after cancelling tasks

The problem comes from closing the loop immediately after cancelling the tasks. As the cancel() docs state “This arranges for a CancelledError to be thrown into the wrapped coroutine on the next cycle through the event loop.” Take this snippet of code: import asyncio import signal async def pending_doom(): await asyncio.sleep(2) print(“>> Cancelling tasks now”) … 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 use asyncio with existing blocking library?

There are (sort of) two questions here: how can I run blocking code asynchronously within a coroutine 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. … Read more

What does the “yield from” syntax do in asyncio and how is it different from “await”

Short answer: yield from is an old way to wait for asyncio’s coroutine. await is a modern way to wait for asyncio’s coroutine. Detailed answer: Python has generators – special kind of functions that produces a sequence of results instead of a single value. Starting with Python 3.3 yield from expression was added. It allows … Read more