简体   繁体   English

龙卷风并发限于6个连接?

[英]Tornado concurrency limited to 6 connections?

I have the following "dummy server" running: 我正在运行以下“虚拟服务器”:

import tornado.web
from tornado.ioloop import IOLoop
from tornado import gen 
import time

@gen.coroutine
def async_sleep(seconds):
    yield gen.Task(IOLoop.instance().add_timeout, time.time() + seconds)

class TestHandler(tornado.web.RequestHandler):

    reqnum = 0

    @gen.coroutine
    def get(self):
        reqnum = TestHandler.reqnum
        TestHandler.reqnum += 1
        for i in range(100):
            print(reqnum,end='')
            if reqnum == 0 : print()
            yield async_sleep(1)
        self.write(str(reqnum))
        self.finish()

if __name__ == '__main__' :

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

    application.listen(9999)
    IOLoop.instance().start()

So this should run fine for tons of concurrent connections, you'd think... But it doesn't. 因此,对于大量的并发连接,这应该可以正常运行,但是事实并非如此。 Starting the server and opening 16 tabs to it, you get: 启动服务器并打开16个选项卡,您将获得:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
120
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520
314520

Which is pretty perplexing. 这真是令人困惑。 What's weirder still is that the correct result is printed on all tabs, 0-15. 奇怪的是,正确的结果打印在所有选项卡上(0-15)。 That's all the output to the console, though. 这就是控制台的所有输出。 No more arrived. 没有更多的到达。

What the heck is happening? 到底发生了什么?

I think it's a combination of two things: 我认为这是两件事的结合:

  1. The browser is limiting the number of connections to the server ( see the FAQ ). 浏览器正在限制与服务器的连接数( 请参阅FAQ )。 If you use separate browsers and not just tabs in one browser you should see more connections. 如果您使用单独的浏览器,而不仅仅是一个浏览器中的选项卡,则应该看到更多的连接。

  2. Python's stdout is line-buffered, so output is only written to the console when there is a newline. Python的stdout是行缓冲的,因此仅在有换行符时才将输出写入控制台。 Once request 0 is finished, you no longer write newlines and so everything else is getting held in the buffer. 请求0完成后,您将不再编写换行符,因此其他所有内容都将保留在缓冲区中。

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

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