Exploring asynchronous programming in Python

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the Responsive LLM Applications with Server-Sent Events course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to Responsive LLM Applications with Server-Sent Events, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course Responsive LLM Applications with Server-Sent Events
  • [00:00 - 00:18] Welcome back. In this lesson, we'll briefly present asang programming and why it matters a lot for use case. Asang continues programming enables non-broken code execution. In Python, asang renews programming is made possible thanks to coroutine and to the asang. io module.

    [00:19 - 01:35] There are multiple tasks to run concurrently within a single thread. Let's see a simple diagram to get better understanding. Let's imagine you have a task 1 which is made of task 1 1 1 2 and 1 3 and you have a task 2 which is which is comprised of task 2 1 and 2 2. Now if you write in synchronous programming which is the standard way of coding, it will be blocking meaning until the task 1 is not finished, the task 2 may not begin. The big problem as you can see as we are wasting a lot of time and nothing can happen until every task is finished for the x1. There is a better way. The better way is asynchronous programming. Every time a sub-task is finished, it yields the process and now it's possible to interleave sub-task. So here we are adding task 2 and then and so on. Now this is very important if you are coding an API. Why? Because you may imagine that each task is a request and if you use synchronous programming, every request will completely block your server so you won't be able to serve several requests at the same time.

    [01:36 - 01:54] And that's why every time you create an API, you want it to be asynchronous. So let's see a bit of Python code corresponding to this diagram to get a better understanding. So here we have a synchronous implementation. We have a task 1 where we sleep for 2 seconds. The task 2 where we sleep for 3 seconds.

    [01:55 - 03:30] Then we call task 1 task 2. What do we output? We see that the task 1 is blocking. Until task 1 is completed, task 2 does not begin. Now let's see a synchronous implementation . As you can see, we are importing a synchronous then we are defining an asian def. So this will task 1 will be a coroutine and then we sleep. And then when we execute those two functions, we can see that task 2 is started before task 1 is completed. So that's a poor asynchronous execution. Why is it extremely relevant in our context? Next imagine that we are calling an API endpoint like open AI and we are streaming token. As you can see, each task is going to be a token generation. And as as you can imagine, it's a very long call lasting dozens of seconds. And each task is simply the generation of one token. And the rest of the time, nothing will happen. If we do not write as a continuous call, we are under blocking the application during dozens of seconds, even though in the vast majority of the time, we are not doing anything. So here, thanks to asynchronous implementation, we are going to be able to stream simple token generation at the same time. Imagine in blue, we have an ailer world text and in red, we have to be or not to be. So that's why we will make sure that in everything we do into a backend, we use an async implementation.

    [03:31 - 03:40] Thanks a lot. See you for our next course, while we see how to implement a async function with first API.