龙卷风长时间轮询请求

编程入门 行业动态 更新时间:2024-10-28 15:22:36
本文介绍了龙卷风长时间轮询请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

以下是我的问题中最简单的示例:

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()

更多推荐

龙卷风长时间轮询请求

本文发布于:2023-11-04 01:31:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1556585.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:长时间   龙卷风

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!