简体   繁体   English

龙卷风web 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.')

Problem: HandlerA blocks HandlerB for 10 seconds. 问题: HandlerA阻止HandlerB 10秒钟。

  1. Browser A handled by HandlerA and waits... 浏览器A由HandlerA处理并等待......
  2. Browser B handled by HandlerB and waits.... till timeout exceptions 浏览器B由HandlerB处理并等待....直到超时异常

Goal 目标

  1. Browser A handled by HandlerA and waits... 浏览器A由HandlerA处理并等待......
  2. Browser B handled by HandlerB and returns 浏览器B由HandlerB处理并返回
  3. HandlerA returns after dequeuing HandlerA在出列后返回

Is this an issue with Non-blocking, async, epoll or sockets? 这是非阻塞,异步,epoll或套接字的问题吗?

Thanks! 谢谢!

UPDATE: 更新:

I updated this code with a new thread to handle the Queue.get_nowait() request. 我使用新线程更新了此代码以处理Queue.get_nowait()请求。 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. 我正在考虑在不久的将来转向epoll风格。

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

Found here Running blocking code in Tornado 在这里找到在Tornado中运行阻止代码

This is Python. 这是Python。 So, time.sleep will always block the flow! 所以, time.sleep将始终阻止流程! In order to call action after 10 seconds with Tornado, you need to use tornado.ioloop.add_timeout function and pass callback as param. 要使用Tornado在10秒后调用操作,您需要使用tornado.ioloop.add_timeout函数并将回调作为参数传递。 Docs for more information . 文档了解更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM