In this video, the focus is on handling events with the help of Asyncio.
- In our previous video, we saw how to use the concurrent.futures Python modules. Welcome to the second video of this section, titled Event Loop Management with Asyncio. In this video, we'll be defining asynchronous tasks, and then we'll run the code, which will show the event loop management. The Python module Asyncio provides facilities to manage events, coroutines, tasks and threads, and synchronization primitives to write concurrent code. The main components and concepts of this module are one, an event loop. The Asyncio model allows a single event loop per process.
Two, coroutines. This is the generalization of the subroutine concept. Also, a coroutine can be suspended during the execution, so that it waits for external processing, some routine in I/O, and returns from the point at which it had stopped when the external processing was done. Three, futures. This defines the future object, such as the concurrent.futures module that represents a computation that has still not been accomplished. Four, tasks. This is a subclass of Asyncio that is used to encapsulate and manage coroutines in a parallel mode.
In this video, the focus is on handling events. In fact, in the context of asynchronous programming, events are very important, since they are inherently asynchronous. Within a computational system, the entity that can generate events is called an event source, while the entity that negotiates to manage an event is called the event handler. Sometimes there may be a third entity called an event loop. It realizes the functionality to manage all the events in a computational code. More precisely, the event loop acts cyclically during the whole execution of the program, and keeps track of events that have occurred within a data structure, to queue and then process them one at a time by invoking the event handler if the main thread is free.
Finally, we report a pseudocode of an event loop manager. All the events in the while loop are caught and then processed by the event handler. The handler that processes an event is the only activity that takes place in the system. When the handler has ended, the control is passed on to the next event that is scheduled. Asyncio provides methods that are used to manage an event loop which are one, loop equals get_event_loop method. Using this, you can get the event loop for the current context. Two, loop.call_later. Time delay, callback, argument.
This arranges for the callback that is to be called after the given time delay seconds. Three, loop.call_soon. Callback, argument. This arranges for a callback that is to be called as soon as possible. The callback is called after call_soon returns, and when the control returns to the event loop. Four, loop.time. This returns the current time as a flat value according to the event loop's internal clock. Five, asyncio.set_event_loop. This sets the event loop for the current context to loop.
Six, asyncio.new_event_loop. This creates and returns a new event loop object according to the policy's rules. Seven, loop.run_forever. This runs until stop is called. In this example, we show you how to use the loop event statements provided by the Asyncio library to build an application that works in an asynchronous mode. Let's consider this code. In this example, we define three asynchronous tasks, where each task calls the subsequent in the order as shown in the figure. To accomplish this, we need to capture the event loop.
Then we schedule the first call to function_1 by the call_soon construct as you can see here. Let's note the definition of function_1 using this code. This defines the asynchronous behavior of the application with the following arguments. One, end_time. This defines the upper time limit within function_1 and makes the call to function_2 through the call_later method. Two, loop. This is the loop event that was captured previously with the get_event_loop method. The task of function_1 is pretty simple, which is to print its name.
But it could also be more computationally intensive. After performing the task, it's compared to loop.time, with the total length of the run. The total number of the cycles is 12, and if it's not past this time, then it is executed with a call_later method, with a delay of one second. For function_2 and function_3 the operation is the same. If the running time expires, then the loop event must end. Let's run this file and see the output. Go to your command prompt and run this file. The output of the code will be something like this. Awesome! So we saw event loop management with Asyncio.
In our next video, we'll see how to handle coroutines with Asyncio.
Note: This course was created by Packt Publishing. We are pleased to host this training in our library.
- Memory organization
- Parallel programming models
- Designing a parallel program and evaluating performance
- Working with threads in Python
- Synchronizing threads and using multithreading
- Spawning a process
- Running a process in the background
- Synchronizing processes
- Using the mpi4py Python module
- Using collective communication
- Reducing operations
- Managing events, tasks, and routines with Asyncio
- Distributing tasks