async and await
Validating code is important, but another important aspect we also need to understand is asynchronous programming. Asynchronous programming allows us to write code that can perform multiple tasks at once without blocking the main thread. This is particularly useful in scenarios where we need to handle I/O-bound operations, such as making API calls or reading from a database.
Let’s talk about async/await, which is a syntax for writing asynchronous code in Python. You should use async/await when you want to write non-blocking code that can handle many tasks at once. If your code is blocking, then the end user experience suffers, as they have to wait for each task to complete before moving on. Imagine the problem multiplying on a web server with many users. Okay, so what do we need to know? First, let’s start with the concepts:
- coroutine: A so-called coroutine is created when you mark up a function with- async def. That means this function...
 
                                             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
     
         
                 
                 
                 
                 
                 
                 
                 
                 
                