gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 09:55:50 25 4
gpt4 key购买 nike

我想为多个请求共享一个 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 个并发客户端进行基准测试时,出现以下错误:

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

最佳答案

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

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

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

关于python - Tornado :如何为多个请求共享 pymongo 连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18939474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com