Chapter 17
What is asynchronous programming? What advantages does it provide?
Asynchronous programming is a model of programming that takes advantage of coordinating computing tasks to overlap the waiting and processing times. If successfully implemented, asynchronous programming provides both responsiveness and an improvement in speed, as compared to synchronous programming.
What are the main elements in an asynchronous program? How do they interact with each other?
There are three main components of an asynchronous program: the event loop, the coroutines, and the futures. The event loop is in charge of scheduling and managing coroutines by using its task queue; the coroutines are computing tasks that are to be executed asynchronously, and each coroutine has to specify, inside its function, exactly where it will give the execution flow back to the event loop (that is, the task-switching event); the futures are placeholder objects that contain the results obtained from the coroutines.
What are the async
and await
keywords? What purposes do they serve?
The async
and await
keywords are provided by the Python language as a way to implement asynchronous programming on a low level. The async
keyword is placed in front of a function, in order to declare it as a coroutine, while the await
keyword specifies the task-switching events.
What options does the asyncio
module provide, in terms of the implementation of asynchronous programming?
The asyncio
module provides an easy-to-use API and an intuitive framework to implement asynchronous programs; additionally, this framework makes the asynchronous code just as readable as synchronous code, which is generally quite rare in asynchronous programming.
What are the improvements, in regards to asynchronous programming, provided in Python 3.7?
Python 3.7 comes with improvements in the API that initiates and runs the main event loop of asynchronous programs, while reserving async
and await
as official Python keywords.
What are blocking functions? Why do they pose a problem for traditional asynchronous programming?
Blocking functions have non-stop execution, and therefore, they prevent any attempts to cooperatively switch tasks in an asynchronous program. If forced to release the execution flow back to the event loop, blocking functions will simply halt their execution until it is their turn to run again. While still achieving better responsiveness, in this case, asynchronous programming fails to improve the speed of the program; in fact, the asynchronous version of the program takes longer to finish executing than the synchronous version, most of the time, due to various overheads.
How doesconcurrent.futures
provide a solution to blocking functions for asynchronous programming? What options does it provide?
The concurrent.futures
module implements threading and multiprocessing for the execution of coroutines in an asynchronous program. It provides the ThreadPoolExecutor
and ProcessPoolExecutor
for asynchronous programming in separate threads and separate processes, respectively.