0
以下工作一度是我的代碼:蟒蛇龍捲風AsyncHTTPClient讀取請求的線程
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."
任何幫助將不勝感激..
看起來像我t不是這個失敗的代碼,但服務器沒有響應3000ms超時。您是否嘗試將網址指向某處,至少會始終像google.com一樣迴應? – barny
我也試過google.com,它只能運行一次@barny –
爲什麼不使用[tornado io_loop](http://tornado.readthedocs.org/en/latest/ioloop.html)? – rebeling