简体   繁体   中英

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

We are using Tornado for providing MongoDB queries results.
The problem is that a query blocks the Http Server until it returns, which causes slow responses and in some cases - timeouts.
Our solution is to use Gevent to handle the requests async, like so:

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 . Using it should be better than mixing two different frameworks together.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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