Python Asynchronous Programming - 5 - Async Event-Loop

Опубликовано: 11 Март 2025
на канале: buildwithpython
5,546
52

In the last video we learned how to create co-routines. So lets get stared with a recap. We first imported the asynchio module. Now asyncho is just a library whiich will help us execute those co-routines in an asynchrounours fasion.

Then we created a co-routine with the async keyword and the created the function, lets just call it main and inside this main co-routine function lets just print out started. To execute this co-routine funtion we are going to create an eevent loop using asyncio.run() function it takes a co-routine function inside it which in our case is the main function. So lets try executing the program and see if it works.

as you can see it printed the the text 'started'.
Now what excactly is an asynch event loop. In simple words whenever you need to execute a co-routine you need to create an event loop which will take a co-routine function and exeute it to completion.

Now obviously there is much more depthj to asynch event loops which you dont need to understand. So the next part is optional because its o nly useful for people who are creating there own frameworks. For example if were creating something like django or flask you probably need it, to monitor the progress of tasks. But dont get me wrong its still good to know.

So when we type in asynchio.run it actualy does a lot of work behind the scenes. It created an event loop for us. Executes the co-routines. Monitors the co-routine and finally ends the event loop. We didnt have to do any of this in our example.

But what if we wanted to? So lets create our own event loop.

Aim -
1) Create our own event loop
2) Create a co-routine
3) Execute that co-routine using our own event loop

To do that lets create a new variable, lets just call it loop and inside that we are go use the asyncio.new_event_loop function to create our own event loop. lets print out this loop variable to see what it contains.

Running the example creates the event loop, then reports the details of the object.

We can see that in this case the event loop has the type of ProactorEventLoop object and is not running, but is also not closed.
ProactorEventLoop is the just the default name on Windows. if you are running linux or mac you will se smthing different.

So now that we have access to our own event loop how do you run a coroutine or task in the event Loop.
so first lets create a task. we are going to create a new variable, kets just calll it task1 and we are going to give it a task of sleeping for 2 seconds, by writing asyncio.sleep and 2 as the argument. Now this might not look like but asyncio.sleep actually creates a co-routine behind the scenes. and how do you execute a co-routine? by using event loop. Let sjust use our loop variable and we are going to use the run_until_complete function to exetute this task. then lets just print out done.

So if we run this program. It will first create an event loop. Store it in loop variable. Create the sleep co-routine. then execute the sleep co-routine to completion becuase we used to run_until_comlete functionality. and sleep for 2 seconds. and then finally it prints out done.

I hope you have a better understanding of how the asyncio.run function works behind the scenes. We are slowly getting the tools to do asychrounous programming. I know it must frustrating to not straight get into it. But trust you will be able to understand it so much better if you learn all the basics first. So guys this is pretty much it in this ill see you in the next one.

Full playlist -    • Python Asynchronous Programming with ...  

Subscribe -    / @buildwithpython  

Instagram -   / buildwithpython