以下是我的代码:
import sys
import os
import imp
import time
import csv
import json
import uuid
import threading
import urllib
from tornado.web import StaticFileHandler
from tornado.httpclient import AsyncHTTPClient
from 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()
在上面的代码中,当第一次调用call_alert_url时,我收到了响应,但之后的任何后续调用都会导致超时。
以下是输出:
http://192.168.0.148:8080/alerts
stackoverflow
HTTPResponse(_body=None,buffer=<_io.BytesIO object at 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 at 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 at 0x10a6dcf10>,request_time=20.002495050430298,time_info={})
None
nitro.ioloop也有龙卷风io_loop:
from tornado.ioloop import IOLoop as 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."
任何帮助将不胜感激..