October 2018
Beginner to intermediate
466 pages
12h 2m
English
AsyncIO provides its own version of the futures library to allow us to run code in a separate thread or process when there isn't an appropriate non-blocking call to be made. This allows us to combine threads and processes with the asynchronous model. One of the more useful applications of this feature is to get the best of both worlds when an application has bursts of I/O-bound and CPU-bound activity. The I/O-bound portions can happen in the event loop, while the CPU-intensive work can be spun off to a different process. To illustrate this, let's implement sorting as a service using AsyncIO:
import asyncioimport jsonfrom concurrent.futures import ProcessPoolExecutordef sort_in_process(data): nums = json.loads(data.decode()) ...
Read now
Unlock full access