Ich bin Neuling auf Tornado und Python. Vor ein paar Tagen fing ich an, eine nicht blockierende Ruhe-API zu schreiben, aber ich konnte die Mission noch nicht erfüllen. Wenn ich zwei Anfragen gleichzeitig an diesen Endpunkt "localhost: 8080/async" sende, reagiert die zweite Anfrage nach 20 Sekunden! Das erklärt, dass ich etwas falsch mache.Python Tornado gen.coroutine Blöcke Anfrage
MAX_WORKERS = 4
class ASYNCHandler(tornado.web.RequestHandler):
executor = ThreadPoolExecutor(max_workers=MAX_WORKERS)
counter = 0
def pow_task(self, x, y):
time.sleep(10)
return pow(x,y)
async def background_task(self):
future = ASYNCHandler.executor.submit(self.pow_task, 2, 3)
return future
@gen.coroutine
def get(self, *args, **kwargs):
future = yield from self.background_task()
response= dumps({"result":future.result()}, default=json_util.default)
print(response)
application = tornado.web.Application([
('/async', ASYNCHandler),
('/sync', SYNCHandler),
], db=db, debug=True)
application.listen(8888)
tornado.ioloop.IOLoop.current().start()