Managing the event loop with asyncio

The asyncio Python module provides facilities for managing events, coroutines, tasks, as well as threads, and synchronization primitives for writing concurrent code.

The main components of this module are as follows:

  • Event loop: The asyncio module allows one event loop per process. This is the entity that deals with managing and distributing the execution of different tasks. In particular, this registers the tasks and manages them by switching the control flow from one task to another.
  • Coroutines: This is a generalization of the concept of the subroutine. Also, a coroutine can be suspended during execution to wait for external processing (some routine in I/O) and return from the point it had stopped at when the external processing is done.
  • Futures: This defines the Future object exactly like the concurrent.futures module. It represents a computation that has still not been accomplished.
  • Tasks: This is a subclass of asyncio that is used to encapsulate and manage coroutines in a parallel mode.

In this recipe, the focus is on the concept of events and event management (namely, event loops) within a software program.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.64.128