python - 使用 Tornado 发送多个异步发布请求

标签 python http tornado

在 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/

相关文章:

python - 我想将列表的顺序更改为[1,2,3,4] 为[2,3,4,1],[3,4,2,1] 和[4,3,2,1]

java - 如何在后台查看Cloudfundy Java客户端发送的请求和响应(Api Mocking)

python - gevent和tornado结合如何提高性能?

python - 在 Tornado 下运行 Pyramid WSGI 应用程序

python - 给定三个坐标点,如何检测它们之间的角度何时超过 180 度?

python - python中从垂直到水平输出?

python - 获取组名称作为图形 matplotlib 中的轴

json - Flutter Dart http Type Response不是字符串错误的子类型

http - 如何在 nginx 中禁用日志记录图像但仍然允许获取请求?

python - 为什么在使用可尾部 MotorCursor 并关闭 Motor 客户端连接时会出现被忽略的异常?