Building a CPU monitor

Now that we have a grasp on the main reactive programming concepts, we can implement a sample application. In this subsection, we will implement a monitor that will give us real-time information about our CPU usage and is capable of detecting spikes.

The complete code for the CPU monitor can be found in the cpu_monitor.py file.

As a first step, let's implement a data source. We will use the psutil module that provides a function, psutil.cpu_percent, that returns the latest available CPU usage as a percent (and doesn't block):

    import psutil    psutil.cpu_percent()    # Result: 9.7

Since we are developing a monitor, we would like to sample this information over a few time intervals. To accomplish this we can use the familiar ...

Get Python High Performance - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.