geofurb
geofurb

Reputation: 489

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:

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. That's all the output to the console, though. No more arrived.

What the heck is happening?

Upvotes: 0

Views: 857

Answers (1)

Ben Darnell
Ben Darnell

Reputation: 22154

I think it's a combination of two things:

  1. The browser is limiting the number of connections to the server (see the 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. Once request 0 is finished, you no longer write newlines and so everything else is getting held in the buffer.

Upvotes: 4

Related Questions