Import the usual suspects:
%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt
We will also requre tools from these Python libraries:
import psutil
from time import sleep
from multiprocessing import Process
We write a function that will monitor the instantaneous load on each core of the current computer's processor and plot this using matplotlib.
def htop():
# HACK: printing makes figure appear in notebook
print('')
fig, ax = plt.subplots(1,1)
#plt.ion()
fig.show()
fig.canvas.draw()
ncpu = psutil.cpu_count()
while True:
ax.clear()
ax.barh(range(ncpu), psutil.cpu_percent(percpu=True))
ax.set_xlim(0,100)
ax.set_xlabel('CPU %')
ax.set_yticks(range(ncpu))
fig.canvas.draw()
sleep(1)
We call this function using a thread so that it will continue running in the background whilst we continue doing other things in the notebook.
## MAIN
t = Process(target=htop)
t.daemon = True
t.start()