Some of my handlers are used to return json, I write a new handler to get
the jsons and comput them to generate new json, but i can't do this, it
seems to no effect. But when i fetch them in a alone python script, it runs
well. Cos i use gevent and urlopen? but I don't know how to do this use
tornado's method. Please help.
some code:
def fetch_info(url, n):
fetch_url = str(url) + str(n)
raw_info = urlopen(fetch_url).read()
info = tornado.escape.json_decode(raw_info)
return info
def getinfo(n):
url = 'http://localhost:8888/serverdemic/'
info_dict = fetch_info(url, n)
down_speed = info_dict['net_info']['load']
up_speed = info_dict['net_info']['up']
return (down_speed, up_speed)
def getstatus():
servers = [i['machineNo'] for i in db.nebula_machine.find()]
jobs = [gevent.spawn(fetch_info, 'http://127.0.0.1:8888/serverstatus/',
i) for i in servers]
gevent.joinall(jobs)
ids = [job.value['id'] for job in jobs if job.value['status'] != 0]
return ids
class TotalNetHandler(BaseHandler):
def get(self):
jobs = [gevent.spawn(getinfo, i) for i in getstatus()]
gevent.joinall(jobs)
raw_info = [job.value for job in jobs]
down_up = zip(*raw_info)
downup_sum = [sum(i) for i in down_up]
info = {'down': downup_sum[0], 'up': downup_sum[1]}
self.write(tornado.escape.json_encode(info))