未释放PyMongo连接:错误,因为没有更多到MongoDB的连接可用
我有一个脚本,它使用了本地运行的MongoDB的所有可用连接,然后出现以下错误消息:未释放PyMongo连接:错误,因为没有更多到MongoDB的连接可用,mongodb,pymongo,Mongodb,Pymongo,我有一个脚本,它使用了本地运行的MongoDB的所有可用连接,然后出现以下错误消息: pymongo.errors.AutoReconnect: [Errno 4] Interrupted system call /var/log/mongodb/mongod.log中的错误消息是: [initandlisten] connection refused because too many open connections: 819 Mongo Shell在调用脚本时提供以下输出: > d
pymongo.errors.AutoReconnect: [Errno 4] Interrupted system call
/var/log/mongodb/mongod.log中的错误消息是:
[initandlisten] connection refused because too many open connections: 819
Mongo Shell在调用脚本时提供以下输出:
> db.serverStatus().connections
{ "current" : 1, "available" : 818 } // Script not yet started
> db.serverStatus().connections
{ "current" : 400, "available" : 419 } // Losing connections
> db.serverStatus().connections
{ "current" : 638, "available" : 181 }
> db.serverStatus().connections
{ "current" : 804, "available" : 15 }
> db.serverStatus().connections
{ "current" : 819, "available" : 0 } // Script terminates
> db.serverStatus().connections
{ "current" : 1, "available" : 818 } // Script terminated
我无法发布原始脚本,但下面的“净化”片段有望给出一个想法。基本上,我在几个集合中运行各种不同的查询和更新
最重要的是,11条数据库语句中只有5条有时不会释放它们的连接。其他6条语句将始终保留与语句之前一样多的空闲连接
mc = pymongo.Connection()
db = mc.database
def available():
""" Return the number of available connections """
return db.command("serverStatus")['connections']['available']
def process():
while True:
# ...
# Connections lost:
conns = available()
coll_a = db.coll_a.find_and_modify(
query={'x': x},
update={'$pop': {'x': -1}},
fields={'x': 1})
if conns > available():
print('Fewer connections')
# ...
# No connections lost:
db.coll_a.update({'x': x}, {'$pullAll': {'x': [x]}})
# ...
# No connections lost:
coll_d = db.coll_d.find_and_modify(
query={'x': x,},
update={'$set': {'x': x}},
fields={'x': 1})
# ...
# No connections lost:
coll_b_cursor = db.coll_b.find({'x': x}, {'x': 1})
# ...
# No connections lost:
coll_e_cursor = db.coll_e.find({'$or': [{'x': x}, {'x': x}]})
# ...
for item in coll_e_cursor:
# No connections lost:
coll_b = db.coll_b.find({'x': x}).count()
# ...
# No connections lost:
coll_b_cursor = db.coll_b.find({'x': x}, {'x': x})
# ...
for x in y:
if x:
# Connections lost:
conns = available()
db.coll_b.update({'x': x}, {'$unset': {'x': 1}})
if conns > available():
print('Fewer connections')
# ...
# Connections lost:
conns = available()
coll_b = db.coll_b.find_and_modify(
query={'x': x},
update={'$set': {'x': x}},
fields={'x': 1},
upsert=True)
if conns > available():
print('Fewer connections')
# ...
# Connections lost:
conns = available()
coll_c_1 = db.coll_c.find_one({'x': x})
coll_c_2 = db.coll_c.find_one({'x': x})
if conns > available():
print('Fewer connections')
# ...
# Connections lost:
conns = available()
db.coll_b.update({'x': x}, {'$unset': {'x': 1}})
if conns > available():
print('Fewer connections')
- 不释放连接的语句与释放连接的语句之间有什么区别
- PyMongo或MongoDB在任何情况下都不应该释放连接吗
MongoDB:2.0.6连接并没有在看起来的地方丢失,而是在什么时候丢失 我忽略了进程()中某个地方启动的线程: 此线程进行了数据库调用 进程()中的while循环足够快,因此许多线程同时运行。正在使用所有可用的数据库连接
在不启动这些线程的情况下,可用数据库连接的数量保持不变。您的
pymongo.Connection
调用是什么样子的?尝试将其更改为pymongo.Connection(safe=True)
。我认为你的写操作远远超过了数据库,你正在消耗大量的连接。不幸的是,这并不能解决这个问题。不过,谢谢你!我按照您的方式运行脚本,并且我的连接数保持不变。你确定没有其他正在运行的东西会占用连接吗?是的,我确定。它是唯一正在运行的脚本。可能问题只会与我的特定数据或正在处理的数据量结合起来出现。
threading.Thread(target=target, args=(args)).start()