简体   繁体   English

为什么龙卷风在python中阻止了我的请求

[英]why is tornado in python blocking my requests

I have this fairly simple code, taken from an example. 我有一个非常简单的代码,摘自一个示例。

#!/usr/bin/python

import tornado.ioloop
import tornado.web
import tornado.gen
import time

class MainHandler(tornado.web.RequestHandler):

        @tornado.web.asynchronous
        @tornado.gen.engine
        def get(self):
                for i in range(1,10):
                        self.write("%d<br>" % i)
                        self.flush()
                        yield tornado.gen.Task(tornado.ioloop.IOLoop.instance().add_timeout, time.time() + 1)
                self.finish()

application = tornado.web.Application([
    (r"/", MainHandler),
])

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

It isn't behaving how i'm expecting it to. 它不符合我的期望。 If i open a browser window and point it to localhost:8888/, it will show 1 [pause 1 sec] 2 [pause 1 sec], etc. If i open a second tab doing the same request, it will block until the first request is finished. 如果我打开浏览器窗口并将其指向localhost:8888 /,它将显示1 [pause 1 sec] 2 [pause 1 sec],依此类推。如果我打开另一个执行相同请求的选项卡,它将阻塞直到第一个请求已完成。 What am i missing? 我想念什么?

似乎我应该使用其他浏览器或隐身窗口。

@Germano is right, it is the chrome shares the same connection for the same url.you can test with below code. @Germano是正确的,它是chrome共享相同的连接,并且使用相同的url。您可以使用以下代码进行测试。

#coding:utf8
from tornado import ioloop
from tornado import web
from tornado import gen

class MainHandler(web.RequestHandler):

    @gen.coroutine
    def get(self):
        client_address = self.request.connection.stream.socket.getpeername()
        print repr(client_address), 'enter'
        yield gen.sleep(10)
        self.write("Hello, world")
        print repr(client_address), 'leave'
        self.finish()

if __name__ == "__main__":
    application = web.Application([
        (r"/", MainHandler),
    ])
    application.listen(8888)
    ioloop.IOLoop.current().start()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM