Tornado:如何为多个请求共享pymongo连接?

2024-06-16 09:53:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我想为多个请求共享一个MongoDB连接。这就是我现在所拥有的,但是看起来它正在为每个请求创建一个新的连接。在

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个并发客户端进行基准测试时,我得到以下错误:

^{pr2}$

Tags: selfclientwebidonresponsemongodbdef
1条回答
网友
1楼 · 发布于 2024-06-16 09:53:41

maxconnections参数不用于缓冲要由现有连接池重用的请求。相反,它的存在只是为了确保,如果需要,应用程序不会消耗无限量的资源。有关此行为的更多讨论,请参见https://github.com/bitly/asyncmongo/pull/45。这个pull请求似乎提供了您想要的行为。您可以使用以下方法安装asyncmongo及其修订版:

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

或者,您可以在Tornado设置中限制应用程序的并发连接数,或者通过nginx(参见httplimitconmodule)

相关问题 更多 >