Tornado 不能异步工作 [英] Tornado doesn't work asynchronously

查看:69
本文介绍了Tornado 不能异步工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Tornado 编写一个非阻塞 api.但是当我在本地尝试时,第一个请求阻止了 API.我尝试使用不同的浏览器,但结果是一样的.

I am trying to write a non-blocking api with tornado. But when I try it on local the first request is blocking the API. I tried to use different browsers but the result is same.

我打开了 chrome 和 Firefox.在 chrome 上我去http://localhost:8888/test-first当它正在加载时,我立即去 http://localhost:8888/test-second 来自 Firefox.

I opened chrome and firefox. On chrome I go http://localhost:8888/test-first and while it is loading I immediatly go http://localhost:8888/test-second from firefox.

我期待 Firefox 的请求会在另一个仍在加载时得到答复.但是他们都在等待,当第一个请求完成时,他们都完成了.

I am expecting the request from firefox would be answered while the other one is still loading. But both of them are waiting and when the first request is done both of them finishes.

我的代码和输出:

期望控制台输出:

First request: 2017-11-22 22:23:22.093497
Second request: 2017-11-22 22:23:24.580052
Second answer: 2017-11-22 22:23:25.580509
First answer: 2017-11-22 22:23:37.579263

控制台输出

First request: 2017-11-22 22:23:22.093497
First answer: 2017-11-22 22:23:37.579263
Second request: 2017-11-22 22:23:37.580052
Second answer: 2017-11-22 22:23:37.580509

test_first.py:

import tornado.web
import datetime


class First(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    async def get(self):
        print("First request: " + str(datetime.datetime.now()))
        for _ in range(1000000000):
            pass
        self.set_status(200)
        self.write("OK")
        self.finish()
        print("First answer: " + str(datetime.datetime.now()))

test_second.py:

import tornado.web
import datetime


class Second(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    async def get(self):
        print("Second request: " + str(datetime.datetime.now()))
        self.set_status(200)
        self.write("OK")
        self.finish()
        print("Second answer: " + str(datetime.datetime.now()))

app.py:

import tornado
from test_first import First
from test_second import Second


class Application(tornado.web.Application):
    def __init__(self):
        ENDPOINTS = [
            # USERS #
            (r"/test-first", First),
            (r"/test-second", Second)
        ]

        SETTINGS = {
            "debug": True,
            "autoreload": True,
            "serve_traceback": True,
            "compress_response": True
        }

        tornado.web.Application.__init__(self, ENDPOINTS, SETTINGS)


if __name__ == "__main__":
    print("dinliyor...")
    Application().listen(8888)
    tornado.ioloop.IOLoop.instance().start()

推荐答案

for _ in range(1000000000):
        pass

这是一个 CPU 密集型任务.因此,它会阻止整个服务器.Tornado 和几乎所有其他异步库都用于进行异步网络 I/O,这意味着数据进来,数据出去 - 没有繁重的 CPU 任务.

This is a CPU intensive task. So, it blocks the whole server. Tornado, and almost every other async library, is for doing asynchronous network I/O, which means, data comes in, data goes out - no heavy CPU tasks.

要执行 CPU 密集型阻塞任务,您必须在单独的进程或线程中运行它,以免它阻塞服务器.

To perform a CPU bound blocking task, you'll have to run it in a separate process, or thread so that it doesn't block the server.

但无论如何,要获得预期的输出,您可以使用 tornado.gen.sleep 暂停 First 处理程序.

But anyway, to get the output as you expect, you can pause the First handler using tornado.gen.sleep.

class First(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    async def get(self):
        print("First request: " + str(datetime.datetime.now()))

        await tornado.gen.sleep(5) # sleep for 5 seconds

        self.set_status(200)
        ...

这篇关于Tornado 不能异步工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆