Details about the async def syntax for path operation functions and some background about asynchronous code, concurrency, and parallelism. If you are

Concurrency and async / await¶

submited by
Style Pass
2024-05-05 00:00:08

Details about the async def syntax for path operation functions and some background about asynchronous code, concurrency, and parallelism.

If you are using a third party library that communicates with something (a database, an API, the file system, etc.) and doesn't have support for using await, (this is currently the case for most database libraries), then declare your path operation functions as normally, with just def, like:

Note: You can mix def and async def in your path operation functions as much as you need and define each one using the best option for you. FastAPI will do the right thing with them.

Modern versions of Python have support for "asynchronous code" using something called "coroutines", with async and await syntax.

Asynchronous code just means that the language 💬 has a way to tell the computer / program 🤖 that at some point in the code, it 🤖 will have to wait for something else to finish somewhere else. Let's say that something else is called "slow-file" 📝.

Then the computer / program 🤖 will come back every time it has a chance because it's waiting again, or whenever it 🤖 finished all the work it had at that point. And it 🤖 will see if any of the tasks it was waiting for have already finished, doing whatever it had to do.

Leave a Comment
Related Posts