简体   繁体   中英

Tornado: How to share pymongo connection for multiple requests?

I want to share a MongoDB connection for multiple requests. This is what I have now, but looks like it's creating a new connection for each request.

dbasync = asyncmongo.Client(pool_id='mydb', host='127.0.0.1', port=27017, maxcached=10, maxconnections=50, dbname='bench')

@route('/readAsync')
class ReadAllAsynchHandler(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    def get(self):        
        print("getting sessions")
        dbasync["ss"].find({}, callback=self._on_response)

    def _on_response(self, response, error):
        print("on response: %s" % response)
        if error:
            raise tornado.web.HTTPError(500)
        self.finish(SS_TEMPLATE.generate(sessions=response))

When benchmarking with 1000 concurrent clients, I get these errors:

Traceback (most recent call last):
  File "/home/ubuntu/envs/myproj/local/lib/python2.7/site-packages/tornado/web.py", line 1115, in _stack_context_handle_exception
    raise_exc_info((type, value, traceback))
  File "/home/ubuntu/envs/myproj/local/lib/python2.7/site-packages/tornado/web.py", line 1298, in wrapper
    result = method(self, *args, **kwargs)
  File "bench.py", line 29, in get
    dbasync["ss"].find({}, callback=self._on_response)
  File "/home/ubuntu/envs/myproj/local/lib/python2.7/site-packages/asyncmongo/cursor.py", line 380, in find
    connection = self.__pool.connection()
  File "/home/ubuntu/envs/myproj/local/lib/python2.7/site-packages/asyncmongo/pool.py", line 116, in connection
    raise TooManyConnections("%d connections are already equal to the max: %d" % (self._connections, self._maxconnections))
TooManyConnections: 50 connections are already equal to the max: 50

DEBUG:root:dropping connection. connection pool (10) is full. maxcached 10

The maxconnections parameter does not serve to buffer up requests to be reused by the existing connection pool. Rather, it just exists to make sure that, if desired, your application will not consume an unbounded amount of resources. For some more discussion of this behavior, see https://github.com/bitly/asyncmongo/pull/45 . This pull request appears to provide the behavior that you desire. You could install asyncmongo with his revisions using something like:

pip install git+git://github.com/ceymard/asyncmongo.git@7a8e6f6f446d71f8fd4f17de48994c0b6bee72ee

Alternatively, you may be able to limit the number of concurrent connections to your application somewhere in the Tornado settings, or else via, say, nginx (see HttpLimitConnModule)

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