Profiling CPU usage in asynchronous code
In Chapter 3 we used a profiler to obtain some information about resource usage from a multithreaded Python script. In asynchronous contexts deterministic profilers (like the standard cProfile) are not useful because they don't take in account the waiting time. In addition to the previously mentioned py-spy, there is another open-source tool called Scalene that is useful for measuring CPU, memory and even GPU usage.We are going to use the Command Line Interface (CLI) provided by Scalene, but there is also a web interface and the programmatic API which allows profile-specific functions/methods to be invoked using the @profile annotation. Scalene provides information about specific lines of code that use CPU/GPU and memory resources. For CPU, Scalene provides three measures of usage as a percentage of the total time: first, the time executing python code; second, the time used to execute native code (like C linked libraries); and finally, the...