Python 为什么tornado中的asynchttpclient不立即发送请求?
有一次,我需要很多时间来提交一个非阻塞请求,用2秒钟的时间,用龙卷风写一个例子,帮帮我 我有一个python服务器客户端程序,它使用Tornado Server.py:Python 为什么tornado中的asynchttpclient不立即发送请求?,python,tornado,nonblocking,asynchttpclient,Python,Tornado,Nonblocking,Asynchttpclient,有一次,我需要很多时间来提交一个非阻塞请求,用2秒钟的时间,用龙卷风写一个例子,帮帮我 我有一个python服务器客户端程序,它使用Tornado Server.py: import tornado.httpserver import tornado.ioloop import random import time def handle_request(request): t = random.randint(1, 10) / 10. _str = "%s rep_time:
import tornado.httpserver
import tornado.ioloop
import random
import time
def handle_request(request):
t = random.randint(1, 10) / 10.
_str = "%s rep_time: %s delay %s" % (request.body, time.time(), t)
time.sleep(t)
request.write('HTTP/1.1 200 OK\r\nContent-Length: %s\r\n\r\n%s' % (len(_str), _str))
request.finish()
http_server = tornado.httpserver.HTTPServer(handle_request)
http_server.listen(8888)
print "server start..."
tornado.ioloop.IOLoop.instance().start()
client.py:
# -*- coding: utf-8 -*-
from tornado.httpclient import AsyncHTTPClient, HTTPRequest
import tornado.ioloop
import time
from tornado.log import gen_log
from tornado import gen
from tornado.options import parse_command_line
import datetime
@gen.coroutine
def handle_requst(response):
if response.body:
gen_log.info("response body: %s" % response.body)
@gen.coroutine
def send_request(num):
yield AsyncHTTPClient().fetch("http://localhost:8888", handle_requst, method="POST", body="req_time: %s no.: %s" % (time.time(), num))
@gen.coroutine
def run():
begin_time = int(time.time() + 1)
while True:
yield gen.Task(tornado.ioloop.IOLoop.current().add_timeout, datetime.timedelta(seconds=1))
now_time = int(time.time())
if now_time == begin_time:
gen_log.info('begin_time:%s' % time.time())
num = 0
while True:
num = num + 1
if num < 10:
#Begin submitting data
send_request(num)
# Submit two seconds
if time.time() > (begin_time + 2):
break
break
gen_log.info('end_time:%s' % time.time())
if __name__ == '__main__':
parse_command_line()
tornado.ioloop.IOLoop.instance().add_callback(run)
tornado.ioloop.IOLoop.instance().start()
开始时间:32秒
结束时间:34秒
请求时间:32秒
服务器接受时间:34s
正如您所看到的,服务器接受时间是34秒,我希望服务器接受时间大约是32秒。首先,我没有安装tornado进行检查,但是如果您更改
yield AsyncHTTPClient().fetch("http://localhost:8888", handle_requst, method="POST", body="req_time: %s no.: %s" % (time.time(), num))
致:
我认为您将获得更多与您期望的第一个请求立即传输的内容一致的信息,而不是等待run方法中断—这需要大约2秒的时间
其次,如果now_time==begin_time:,您正在对times fx进行精确比较,在本例中,您将取整为int,这样它至少可以工作几次。但是使用这样的技术最终会让你的程序崩溃。计算机时钟中的时间并不像你想象的那样精确,有许多进程正在运行,休眠0.1可能超过0.1秒
第三,您似乎期望您的系统与网卡进行即时通信,但这种情况不会发生
第四,您记录为begin_time的不是变量begin_time,您在等待服务器中施加的延迟之前保存了rep_time,但不确定这是否是有意的。time.sleep会阻止IOLoop,这不允许运行任何其他操作,包括AsyncHTTPClient。为了使tornado顺利运行,您必须用非阻塞等效函数替换所有阻塞函数,例如,yield gen.taskioop.current.add_timeout,datetime.timedeltaseconds=1而不是time.sleep1。嗨,Ben,我修改了代码,但仍然没有达到我想要的效果。我不知道怎么办
yield AsyncHTTPClient().fetch("http://localhost:8888", handle_requst, method="POST", body="req_time: %s no.: %s" % (time.time(), num))
AsyncHTTPClient().fetch("http://localhost:8888", handle_requst, method="POST", body="req_time: %s no.: %s" % (time.time(), num))