简体   繁体   English

Tornado + Gevent + PyMongo-这是异步请求的有效且安全的解决方案吗?

[英]Tornado + Gevent + PyMongo - Is this a valid and safe solution for asynchronous requests?

We are using Tornado for providing MongoDB queries results. 我们正在使用Tornado提供MongoDB查询结果。
The problem is that a query blocks the Http Server until it returns, which causes slow responses and in some cases - timeouts. 问题是查询阻塞Http服务器,直到它返回为止,这会导致响应缓慢,在某些情况下会导致超时。
Our solution is to use Gevent to handle the requests async, like so: 我们的解决方案是使用Gevent处理异步请求,如下所示:

from gevent import monkey; monkey.patch_all()
import gevent
from tornado.web import RequestHandler, asynchronous

class Query(RequestHandler):
    @asynchronous
    def get(self):
        def async_query():
            # The following pymongo_client object is created
            # at the application and passed in the 'settings' dict.
            pymongo_client.do_some_long_query()
            self.write('Done')
            self.finish()

        gevent.spawn(async_query)

This actually DOES work, frees the server to accept and handle requests while the query is being executed. 这实际上可以工作,在执行查询时释放服务器以接受和处理请求。
Our concern is about the combination of the three in the "real world". 我们关注的是“现实世界”中三者的结合。
Do they fit together? 它们合在一起吗? or are we missing something that might break in real traffic? 还是我们错过了可能会破坏实际流量的东西?

There is asynchronous library for connecting with MongoDB from Tornado, called Motor . 有一个用于从Tornado与MongoDB连接的异步库,称为Motor Using it should be better than mixing two different frameworks together. 使用它应该比将两个不同的框架混合在一起更好。

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

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