Asyncio API

With the general structure of an asynchronous program in mind, let's consider the specific APIs that the asyncio module and Python provide for the implementation of asynchronous programs. The first foundation for this API is the async and await keywords that were added to Python 3.5. These keywords are used to specify the main elements of an asynchronous program to Python.

Specifically, async is typically put in front of the def keyword when a function is declared. A function with the async keyword in front of it will be interpreted by Python as a coroutine. As we discussed, inside of each coroutine, there has to be a specification regarding when the task switching events will take place. The await keyword is then used to specify where and when, exactly, to give back the flow of execution to the event loop; this is typically done through waiting for another coroutine to produce a result (await coroutine) or through helper functions from the asyncio module, such as the asyncio.sleep() and asyncio.wait() functions.

It is important to note that the async and await keywords are actually provided by Python and are not managed by the asyncio module. This means that asynchronous programming can actually be implemented without asyncio, but, as you will see, asyncio provides a framework and infrastructure to streamline this process, and is therefore the primary tool in Python for the implementation of asynchronous programming.

Specifically, the most commonly used API from the asyncio module is event-loop-managing functionalities. With asyncio, you can start to manipulate your tasks and event loop with intuitive and easy function calls, without extensive boilerplate code. These include the following:

  • asyncio.get_event_loop(): This method returns the event loop for the current context, which is an AbstractEventLoop object. Most of the time, we do not need to worry about this class, as the asyncio module already provides a high-level API to manage our event loops.
  • AbstractEventLoop.create_task(): This method is to be called by an event loop. It adds its input to the current task queue of the calling event loop; the input is typically a coroutine (that is, a function with the async keyword).
  • AbstractEventLoop.run_until_complete(): This method is also to be called by an event loop. It takes in the main coroutine of an asynchronous program and executes it until the corresponding future of the coroutine is returned. While the method initiates the event loop execution, it also blocks all subsequent code following it, until all futures are complete.
  • AbstractEventLoop.run_forever(): This method is somewhat similar to AbstractEventLoop.run_until_complete(), except for the fact that, as suggested by the method name, the calling event loop will run forever, unless the AbstractEventLoop.stop() method is called. So, instead of exiting, the loop will continue to run, even upon obtaining the returned futures.
  • AbstractEventLoop.stop(): This method causes the calling event loop to stop executing and exit at the nearest appropriate opportunity, without causing the whole program to crash.

Aside from these methods, we use a number of non-blocking functions to facilitate the task switching event. These include the following:

  • asyncio.sleep(): While in itself a coroutine, this function creates an additional coroutine that completes after a given time (specified by the input, in seconds). It is typically used as asyncio.sleep(0), to cause an immediate task switching event.
  • asyncio.wait(): This function is also a coroutine, and hence, it can be used to switch tasks. It takes in a sequence (usually a list) of futures and waits for them to complete their execution.
..................Content has been hidden....................

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