Last active
August 29, 2015 14:26
-
-
Save Muffo/07dcad35507daa19b629 to your computer and use it in GitHub Desktop.
CPU usage with bottle.py and psutil
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'muffo' | |
from bottle import route, response, run | |
from time import sleep | |
from psutil import cpu_percent | |
from json import dumps | |
import thread | |
cpu = None | |
def update_cpu(delay): | |
global cpu | |
while True: | |
cpu = cpu_percent(interval=None, percpu=True) | |
print "CPU Thread:", cpu | |
sleep(delay) | |
@route('/cpu') | |
def hello(): | |
global cpu | |
response.content_type = 'application/json' | |
return dumps({"cpu": cpu}) | |
thread.start_new_thread(update_cpu, (1, )) | |
run(host='localhost', port=8080, debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment