[英]Tornado + Gevent + PyMongo - Is this a valid and safe solution for asynchronous requests?
我们正在使用Tornado提供MongoDB查询结果。
问题是查询阻塞Http服务器,直到它返回为止,这会导致响应缓慢,在某些情况下会导致超时。
我们的解决方案是使用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)
这实际上可以工作,在执行查询时释放服务器以接受和处理请求。
我们关注的是“现实世界”中三者的结合。
它们合在一起吗? 还是我们错过了可能会破坏实际流量的东西?
有一个用于从Tornado与MongoDB连接的异步库,称为Motor 。 使用它应该比将两个不同的框架混合在一起更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.