filmov
tv
Python Asynchronous Programming - 5 - Async Event-Loop

Показать описание
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.
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.
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
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 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.
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.
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
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 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.
Комментарии