以下是我的问题中最简单的示例:
Below is the most simple example of my issue:
发出请求时,它将打印Request via GET <__main__.MainHandler object at 0x104041e10>,然后该请求将保持打开状态.好的!但是,当您发出另一个请求时,直到第一个连接完成,它才调用MainHandler.get方法.
When a request is made it will print Request via GET <__main__.MainHandler object at 0x104041e10> and then the request will remain open. Good! However, when you make another request it does not call the MainHandler.get method until the first connection has finished.
如何在使多个请求保持长时间轮询的同时将多个请求带入get方法.我通过每个请求传递参数,这些请求将通过redis从pub/sub获得不同的结果.问题是我一次只能建立一个连接.怎么了?为什么这会阻止其他请求?
How can I get multiple requests into the get method while having them remain long-polling. I'm passing arguments with each request that will get different results from a pub/sub via redis. Issue is that I only get one connection in at a time. Whats wrong? And why is this blocking other requests?
import tornado.ioloop import tornado.web import os class MainHandler(tornado.web.RequestHandler): @tornado.web.asynchronous def get(self): print 'Request via GET', self if __name__ == '__main__': application = tornado.web.Application([ (r"/", MainHandler)]) try: application.listen(int(os.environ.get('PORT', 5000))) tornado.ioloop.IOLoop.instance().start() except KeyboardInterrupt: tornado.ioloop.IOLoop.instance().stop()左图:如上所述.这些请求未按照右图中请求的方式进行处理. 右图,我需要由RequestHandler处理请求(a-d),然后等待发布/订阅发布它们的数据.
Diagram Left: As described in issue above. The requests are not handled in the fashion requested in right diagram. Diagram on the right I need the requests (a-d) to be handled by the RequestHandler and then wait for the pub/sub to announce their data.
a b c d + + + + ++ a b c d | | | | || + + + + | | | | || | | | | | | | | || | | | | | | | | || | | | | | v v v || | | | | +---|-----------------------------+ || +-----|----|---|---|------------------+ | | | || | | | | | | | + RequestHandler| || | + + + + RequestHan. | | | | || | | | | | | +---|-----------------------------+ || +-----|----|---|---|------------------+ +---|-----------------------------+ || +-----|----|---|---|------------------+ | | | || | | | | | | | + Sub/Pub Que | || | v + v v Que | | | | || | | | +---|-----------------------------+ || +----------|--------------------------+ +---|-----------------------------+ || +----------|--------------------------+ | || | | Finished || | Finished v || v || || || || || || || ++如果这是另一种编程语言可以实现的,请告诉我.
If this is accomplishable with another programming language please let me know.
谢谢您的帮助!
推荐答案来自 www.tornadoweb/en/stable/web.html#tornado.web.asynchronous :
tornado.web.asynchronous(方法)
tornado.web.asynchronous(method)
...
如果指定了此装饰器,则当 方法返回.由请求处理程序调用self.finish() 完成HTTP请求.没有这个装饰器,请求是 get()或post()方法返回时自动完成.
If this decorator is given, the response is not finished when the method returns. It is up to the request handler to call self.finish() to finish the HTTP request. Without this decorator, the request is automatically finished when the get() or post() method returns.
您必须明确地完成get方法:
You have to finish get method explicitly:
import tornado.ioloop import tornado.web import tornado.options from tornado.options import define, options define("port", default=8000, help="run on the given port", type=int) class MainHandler(tornado.web.RequestHandler): @tornado.web.asynchronous def get(self): print 'Request via GET', self self.finish() if __name__ == '__main__': application = tornado.web.Application([ (r"/", MainHandler)]) try: application.listen(options.port) tornado.ioloop.IOLoop.instance().start() except KeyboardInterrupt: tornado.ioloop.IOLoop.instance().stop()更多推荐
龙卷风长时间轮询请求
发布评论