我有一个脚本,它耗尽了本地运行的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在调用脚本时提供此输出:
> 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:2.2.1
MongoDB:2.0.6
答案 0 :(得分:1)
连接没有丢失其中似乎 时。
我忽略了一个线程是在process()的某个地方启动的:
threading.Thread(target=target, args=(args)).start()
这个线程进行了数据库调用。
process()中的while循环足够快,以便许多线程同时运行。用完所有可用的数据库连接。
在不启动这些线程的情况下,可用数据库连接的数量保持不变。