Skip to content

Instantly share code, notes, and snippets.

@Muffo
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Muffo/07dcad35507daa19b629 to your computer and use it in GitHub Desktop.
Save Muffo/07dcad35507daa19b629 to your computer and use it in GitHub Desktop.
CPU usage with bottle.py and psutil
__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