简体   繁体   中英

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. 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.

#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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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