python tornado AsyncHTTPClient fetching request working once in thread -
the following code:
import sys import os import imp import time import csv import json import uuid import threading import urllib tornado.web import staticfilehandler tornado.httpclient import asynchttpclient nitro.ioloop import ioloop io_loop = ioloop() data_server_host = "192.168.0.148" class alertsrun(object) : def __init__(self, config) : self._data_server_port = config.data_server_port #print self._data_server_port (8080) self._terra_base_url = "http://%s:%s" % (data_server_host, self._data_server_port) #print self._terra_base_url self._http_client = asynchttpclient() def alerts_thread(self): self.call_alert_url() print "stackoverflow" threading.timer(60, self.alerts_thread).start() def handle_response (self,api_response) : print api_response data = api_response.body print data def call_alert_url(self) : try : options = {} stream_url = "%s/alerts" % (self._terra_base_url) #encoded_parameters = urllib.urlencode(options) print stream_url #http://192.168.0.148:8080/alerts self._http_client.fetch( stream_url, self.handle_response, #method="post", #body=encoded_parameters, request_timeout=3000 ) except exception, e : return def main() : if len(sys.argv) != 2 : print "usage: run-server.py <config-file>" return config_path = sys.argv[1] config = imp.load_source("terra_config", config_path) alertsrun = alertsrun(config) alertsrun.alerts_thread() io_loop.start() if __name__ == "__main__" : main()
in above code,when call_alert_url called first time getting response subsequent calls after results in timeout.
following output:
http://192.168.0.148:8080/alerts stackoverflow httpresponse(_body=none,buffer=<_io.bytesio object @ 0x10a7ace30>,code=200,effective_url='http://192.168.0.148:8080/alerts',error=none,headers={'x-consumed-content-encoding': 'gzip', 'content-length': '40', 'vary': 'accept-encoding', 'server': 'tornadoserver/4.2.1', 'etag': '"0f2247c8e8facfdc08ebbed85e171d0f211cbdcf"', 'date': 'wed, 30 sep 2015 06:15:56 gmt', 'access-control-allow-origin': '*', 'content-type': 'application/json'},reason='ok',request=<tornado.httpclient.httprequest object @ 0x10a7e2d90>,request_time=0.5350480079650879,time_info={}) { "data": "done" } http://192.168.0.148:8080/alerts stackoverflow httpresponse(_body=none,buffer=none,code=599,effective_url='http://192.168.0.148:8080/alerts',error=httperror('http 599: timeout',),headers={},reason='unknown',request=<tornado.httpclient.httprequest object @ 0x10a6dcf10>,request_time=20.002495050430298,time_info={}) none
also nitro.ioloop has tornado io_loop :
from tornado.ioloop import ioloop tornadoioloop __all__ = [ "ioloop" ] class ioloop(object) : none = tornadoioloop.none read = tornadoioloop.read write = tornadoioloop.write error = tornadoioloop.error def __init__(self) : self._tornado_io_loop = tornadoioloop() def inner(self) : return self._tornado_io_loop def close(self, all_fds=false) : self._tornado_io_loop.close(all_fds) def add_handler(self, fd, handler, events) : self._tornado_io_loop.add_handler(fd, handler, events) def update_handler(self, fd, events) : self._tornado_io_loop.update_handler(fd, events) def remove_handler(self, fd) : self._tornado_io_loop.remove_handler(fd) def start(self) : self._tornado_io_loop.start() def stop(self) : self._tornado_io_loop.stop() def time(self) : return self._tornado_io_loop.time() def add_timeout(self, deadline, callback) : return self._tornado_io_loop.add_timeout(deadline, callback) def remove_timeout(self, timeout) : self._tornado_io_loop.remove_timeout(timeout) def add_callback(self, callback, *args, **kwargs) : self._tornado_io_loop.add_callback(callback, *args, **kwargs) def run(self) : try : self.start() except keyboardinterrupt : print "" print "ctrl-c recieved. exiting."
any appreciated..
use tornado.ioloop directly:
import tornado.ioloop io_loop = tornado.ioloop.ioloop.current()
Comments
Post a Comment