2013-09-22 69 views
3

我想共享多個請求的MongoDB連接。這是我現在擁有的,但看起來它爲每個請求創建一個新的連接。龍捲風:如何共享多個請求的pymongo連接?

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)) 

當1000個併發客戶端標杆,我得到這些錯誤:

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 

回答

0

maxconnections參數不起到緩衝了請求由現有的連接池被重新使用。相反,它只是確保如果需要,您的應用程序不會消耗無限量的資源。有關此行爲的更多討論,請參閱https://github.com/bitly/asyncmongo/pull/45。此拉取請求似乎提供您所需的行爲。你可以安裝asyncmongo使用像他的修訂:

pip install git+git://github.com/ceymard/[email protected] 

或者,你可能能夠併發連接數的某處限制爲您的應用程序在龍捲風的設置,或者通過,也就是說,nginx的(見HttpLimitConnModule )