Asyncio and concurrency are powerful tools for building high-performance web applications, especially with frameworks like FastAPI. By utilizing async functions, you can achieve parallelism and speed up your code execution by allowing multiple tasks to run concurrently.
To understand how to achieve concurrency in FastAPI, let’s first understand the basics of async functions and asyncio.
Async functions in Python allow us to define functions that can run independently and not block the rest of our code while waiting for I/O operations. By using the async
keyword before a function definition, we can mark it as asynchronous and await other async functions inside it.
Asyncio is a library in Python that provides tools for building asynchronous applications. It allows us to manage event loops, create tasks, and handle concurrency in a straightforward way. FastAPI also leverages asyncio under the hood, making it a perfect choice for building high-performance web applications.
To use async functions in FastAPI, we can define routes as async functions, and FastAPI will handle the asynchronous execution for us. For example, let’s define a simple async route in FastAPI:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def async_route():
return {"message": "Hello, async world!"}
In the above example, the async_route
function is an async function, and FastAPI will handle the asynchronous execution of this route.
To achieve concurrency in FastAPI, we can leverage asyncio’s asyncio.gather()
function to run multiple async tasks concurrently. This allows us to execute multiple async functions concurrently and wait for all of them to finish.
For example, let’s define two async routes in FastAPI and run them concurrently using asyncio.gather()
:
from fastapi import FastAPI
import asyncio
app = FastAPI()
async def async_task1():
await asyncio.sleep(1)
return "Task 1 done"
async def async_task2():
await asyncio.sleep(2)
return "Task 2 done"
@app.get("/")
async def concurrent_tasks():
task1_result, task2_result = await asyncio.gather(async_task1(), async_task2())
return {"task1": task1_result, "task2": task2_result}
In the above example, we defined two async tasks, async_task1()
and async_task2()
, and used asyncio.gather()
to run them concurrently in the concurrent_tasks
route.
While using async functions and asyncio for concurrency in FastAPI is powerful, there are some things to avoid doing to ensure optimal performance:
-
Avoid blocking I/O operations: Make sure to use async versions of I/O operations (e.g., async database queries) to prevent blocking the event loop.
-
Be mindful of resource usage: Running too many concurrent tasks can lead to high resource usage. Make sure to limit the number of concurrent tasks or use asyncio’s semaphores to control the number of tasks running concurrently.
- Handle errors gracefully: When running multiple tasks concurrently, make sure to handle errors appropriately to prevent crashing the application. Use try-except blocks or asyncio’s error handling mechanisms to catch and handle exceptions.
In conclusion, async functions and asyncio are powerful tools for achieving concurrency in FastAPI. By leveraging async functions and asyncio.gather(), you can run multiple tasks concurrently and improve the performance of your web applications. Just remember to avoid blocking I/O operations, manage resource usage, and handle errors gracefully to ensure optimal performance.
Great video. Thank you very much!
can you send a book you are referring to in your video?
Excelent
Отличное видео
Great video, well done!
finally amazing explanation with good examples
I don't know how how come that you don't have a milion of views
Awesome
Good job comrade
Hi bro, can you make a video of using chunk method
Лучшее видео по FastAPI что я смотрел за последнее время. Кратко и в то же время очень ёмко. Видно что автор глубоко разбирается в вопросе. Респект!
14:50 What about GIL, doesn't it forbid to create multiple threads within 1 process?
This video should have more views 👍
Very interesting
You desire a million views – that is a great in-depth tutorial!
Did you try to use psycopg3 since it provides async?
« writing async in front of every function and hoping for the best » yup that’s me 😂
Is it just me or our good tutor looks like Scarlett Johansson's brother 🤔. This is A really good tutorial by the way. Gives good context for how to write Async code.
Great video, thanks!
@evgenymaksimov 26:09 Of course, we cannot see that it has made a big difference with just a request. If you send thousands of IO-heavy requests, each request will be dedicated to a different thread, and as you said before, the application will lose performance after a while because the threads are memory hungry. If we use coroutines instead, I can say that they are lighter threads, the application will run more efficiently. Actually this may also be an answer to when to use thread and when to use async.
подписался) Жека а че не на русском?)