龙卷风网络的HTTP请求阻止其他请求,如何不阻止其他请求 [英] tornado web http request blocks other requests, how to not block other requests
问题描述
import tornado.web
import Queue
QUEUE = Queue.Queue()
class HandlerA( tornado.web.RequestHandler ):
def get(self):
global QUEUE
self.finish(QUEUE.get_nowait())
class HandlerB( tornado.web.RequestHandler ):
def get(self):
global QUEUE
QUEUE.put('Hello')
self.finish('In queue.')
问题: HandlerA块HandlerB 10秒
Problem: HandlerA blocks HandlerB for 10 seconds.
- 将HandlerA并等待处理...浏览器
- 浏览器甲乙经HandlerB并等待处理,直到....超时异常
目标
- 将HandlerA并等待处理...浏览器
- 浏览程序B和收益
- HandlerA的收益出队后
通过HandlerB处理
- Browser A handled by HandlerA and waits...
- Browser B handled by HandlerB and returns
- HandlerA returns after dequeuing
这是与非阻塞,异步的epoll或插座的问题?
Is this an issue with Non-blocking, async, epoll or sockets?
谢谢!
更新:
我更新了这个code用新的线程来处理Queue.get_nowait()请求。这恐怕是一个可怕的解决方案,考虑到我要去一次有成千上万的请求,因此将有数千个线程一次。我正考虑迁移到的epoll
的风格在不久的将来。
I updated this code with a new thread to handle the Queue.get_nowait() request. Which I fear is a HORRIBLE solution considering I'm going to have thousands of requests at once and would therefore have thousands of threads at once. I'm considering moving to a epoll
style in the near future.
class HandlerA( tornado.web.RequestHandler ):
@tornado.web.asynchronous
def get(self):
thread.start_new_thread(self.get_next)
def get_next(self):
global QUEUE
self.finish(QUEUE.get_nowait())
现在这不是处理它的最佳方式......但至少它是一个开始。
Now this is not the best way to handle it... but at least its a start.
SOLUTION
在这里找到运行阻塞code在龙卷风
推荐答案
这是Python的。因此, time.sleep
总是阻止流动!为了10秒钟后,龙卷风打电话的动作,你需要使用 tornado.ioloop.add_timeout
函数,并传递回调作为参数。 文档以获取更多信息。
This is Python. So, time.sleep
will always block the flow! In order to call action after 10 seconds with Tornado, you need to use tornado.ioloop.add_timeout
function and pass callback as param. Docs for more information.
这篇关于龙卷风网络的HTTP请求阻止其他请求,如何不阻止其他请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!