系统监视器

系统监视器示例

输出:

  1. 75.0 frames per second
  1. import time
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. def get_memory(t):
  5. "Simulate a function that returns system memory"
  6. return 100 * (0.5 + 0.5 * np.sin(0.5 * np.pi * t))
  7. def get_cpu(t):
  8. "Simulate a function that returns cpu usage"
  9. return 100 * (0.5 + 0.5 * np.sin(0.2 * np.pi * (t - 0.25)))
  10. def get_net(t):
  11. "Simulate a function that returns network bandwidth"
  12. return 100 * (0.5 + 0.5 * np.sin(0.7 * np.pi * (t - 0.1)))
  13. def get_stats(t):
  14. return get_memory(t), get_cpu(t), get_net(t)
  15. fig, ax = plt.subplots()
  16. ind = np.arange(1, 4)
  17. # show the figure, but do not block
  18. plt.show(block=False)
  19. pm, pc, pn = plt.bar(ind, get_stats(0))
  20. pm.set_facecolor('r')
  21. pc.set_facecolor('g')
  22. pn.set_facecolor('b')
  23. ax.set_xticks(ind)
  24. ax.set_xticklabels(['Memory', 'CPU', 'Bandwidth'])
  25. ax.set_ylim([0, 100])
  26. ax.set_ylabel('Percent usage')
  27. ax.set_title('System Monitor')
  28. start = time.time()
  29. for i in range(200): # run for a little while
  30. m, c, n = get_stats(i / 10.0)
  31. # update the animated artists
  32. pm.set_height(m)
  33. pc.set_height(c)
  34. pn.set_height(n)
  35. # ask the canvas to re-draw itself the next time it
  36. # has a chance.
  37. # For most of the GUI backends this adds an event to the queue
  38. # of the GUI frameworks event loop.
  39. fig.canvas.draw_idle()
  40. try:
  41. # make sure that the GUI framework has a chance to run its event loop
  42. # and clear any GUI events. This needs to be in a try/except block
  43. # because the default implementation of this method is to raise
  44. # NotImplementedError
  45. fig.canvas.flush_events()
  46. except NotImplementedError:
  47. pass
  48. stop = time.time()
  49. print("{fps:.1f} frames per second".format(fps=200 / (stop - start)))

脚本的总运行时间:(0分钟2.678秒)

下载这个示例