R
R
rail01y2018-02-03 22:54:02
Python
rail01y, 2018-02-03 22:54:02

How to make tornado not block IO?

Good day, can you tell me why the tornado locks IO when executing the following code:

import time
import tornado.web
from tornado.ioloop import IOLoop
from tornado import gen
from tornado.concurrent import run_on_executor
from concurrent.futures import ThreadPoolExecutor

MAX_WORKERS = 4


class TestHandler(tornado.web.RequestHandler):
    executor = ThreadPoolExecutor(max_workers=MAX_WORKERS)

    @run_on_executor
    def background_task(self, i):
        """ This will be executed in `executor` pool. """
        time.sleep(10)
        return i

    @tornado.gen.coroutine
    def get(self):
        """ Request that asynchronously calls background task. """
        print('%s news handler accept' % time.time())
        res = yield self.background_task(time.time())
        self.write(str(res))


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

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

I know about gen.sleep, but how to make this code not block the application?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2018-02-04
@rail01y

I think I know what's going on with you. Everything is fine with your code, not with the test method. Surely you open two tabs in the same browser and send requests from them. But neither Google Chrome nor Mozila Firefox will send a second request to the same address until they receive a response to the first one. Even if you run two windows. But IE11 sends two requests at the same time to the same address from the tabs without any problems. Try running two different browsers, or even better, use curl for tests.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question