在 stackoverflow 上有几个关于 Tornado 的问题 我还没有找到我的问题的答案 我有一个大文本文件,我希望对其进行迭代并将每一行作为 POST http 请求发送。 我希望异步执行(我需要速度快),然后检查请求的响应。
我有这样的东西
http_client = httpclient.AsyncHTTPClient()
with open(filename) as log_file:
for line in log_file:
request = httpclient.HTTPRequest(self.destination,method="POST",headers=self.headers,body=json.dumps(line))
response = http_client.fetch(request, callback=self.handle_request)
查看 tcpdump 这没有做任何事情 我得到的只是一个严肃的“ future ”对象 我还尝试将 fetch 命令放在“yield”中,然后在方法上使用 @gen.coroutine 装饰器时对其进行迭代。 那没有帮助。 谁能告诉我我做错了什么?
谢谢!
最佳答案
下面是在协程中使用“fetch”的方法:
from tornado import gen, httpclient, ioloop
filename = 'filename.txt'
destination = 'http://localhost:5000'
http_client = httpclient.AsyncHTTPClient()
@gen.coroutine
def post():
with open(filename) as log_file:
for line in log_file:
request = httpclient.HTTPRequest(destination,
body=line,
method="POST")
response = yield http_client.fetch(request)
print response
ioloop.IOLoop.current().run_sync(post)
您可以使用接收行并打印它们的小型服务器进行测试:
from tornado import ioloop, web
class MyHandler(web.RequestHandler):
def post(self):
print self.request.body.rstrip()
app = web.Application([
web.URLSpec('/', MyHandler)
])
app.listen(port=5000)
ioloop.IOLoop.current().start()
先运行服务端代码,再运行客户端。
如果您想同时发布最多 10 行日志,请安装 Toro并做:
from tornado import gen, ioloop
from tornado.httpclient import AsyncHTTPClient, HTTPRequest
from toro import JoinableQueue
filename = 'tox.ini'
destination = 'http://localhost:5000'
AsyncHTTPClient.configure("tornado.simple_httpclient.SimpleAsyncHTTPClient",
max_clients=10)
http_client = AsyncHTTPClient()
q = JoinableQueue(maxsize=10)
@gen.coroutine
def read():
with open(filename) as log_file:
for line in log_file:
yield q.put(line)
@gen.coroutine
def post():
while True:
line = yield q.get()
request = HTTPRequest(destination,
body=line,
method="POST")
# Don't yield, just keep going as long as there's work in the queue.
future = http_client.fetch(request)
def done_callback(future):
q.task_done()
try:
print future.result()
except Exception as exc:
print exc
future.add_done_callback(done_callback)
# Start coroutines.
read()
post()
# Arrange to stop loop when queue is finished.
loop = ioloop.IOLoop.current()
join_future = q.join()
def done(future):
loop.stop()
join_future.add_done_callback(done)
loop.start()
关于python - 使用 Tornado 发送多个异步发布请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26891749/