У меня есть куча машин под управлением Mongodb для определения здоровья, и я решил использовать asyncio для ускорения. Что не так с кодом?
__map_shardName_lostTime__ = {}
async def probe_replset(machines):
for node in machines:
ip = node.ip
port = node.port
try:
client = motor.motor_asyncio.AsyncIOMotorClient(ip, port, username="321", password="123", socketTimeoutMS=1000, connectTimeoutMS=1000, serverSelectionTimeoutMS=1000)
replset_status = await client.admin.command("replSetGetStatus")
except pymongo.errors.OperationFailure as e:
continue
except pymongo.errors.ServerSelectionTimeoutError as e:
continue
members = replset_status['members']
for member in members:
# do something with the __map_shardName_lostTime__
__map_shardName_lostTime__[member['ip']] = 1
def sentinel_replset_primary(machine_list):
loop = asyncio.get_event_loop()
tasks = [probe_replset(ins) for ins in machine_list]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
def sentinel():
# get the metas
machine_list = get_metas()
sentinel_replset_primary(machine_list)
if __name__ == '__main__':
l = task.LoopingCall(sentinel)
l.start(30)
reactor.run()
Первый цикл работает хорошо. Но второй цикл показывает следующее сообщение:
/Users/ynx/python/bytedoc_platform/venv/lib/python3.7/site-packages/twisted/internet/defer.py:153: RuntimeWarning: coroutine 'wait' was never awaited
return fail(failure.Failure(captureVars=Deferred.debug))
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
/Users/ynx/python/bytedoc_platform/venv/lib/python3.7/site-packages/twisted/internet/defer.py:153: RuntimeWarning: coroutine 'probe_replset' was never awaited
return fail(failure.Failure(captureVars=Deferred.debug))
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Unhandled error in Deferred:
Traceback (most recent call last):
File "/Users/ynx/python/bytedoc_platform/venv/lib/python3.7/site-packages/twisted/internet/base.py", line 1283, in run
self.mainLoop()
File "/Users/ynx/python/bytedoc_platform/venv/lib/python3.7/site-packages/twisted/internet/base.py", line 1292, in mainLoop
self.runUntilCurrent()
File "/Users/ynx/python/bytedoc_platform/venv/lib/python3.7/site-packages/twisted/internet/base.py", line 913, in runUntilCurrent
call.func(*call.args, **call.kw)
File "/Users/ynx/python/bytedoc_platform/venv/lib/python3.7/site-packages/twisted/internet/task.py", line 239, in __call__
d = defer.maybeDeferred(self.f, *self.a, **self.kw)
--- <exception caught here> ---
File "/Users/ynx/python/bytedoc_platform/venv/lib/python3.7/site-packages/twisted/internet/defer.py", line 151, in maybeDeferred
result = f(*args, **kw)
File "/Users/ynx/python/bytedoc_platform/sentinel/main.py", line 116, in sentinel
sentinel_replset_primary(replset_list)
File "/Users/ynx/python/bytedoc_platform/sentinel/main.py", line 96, in sentinel_replset_primary
loop.run_until_complete(asyncio.wait(tasks))
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/base_events.py", line 560, in run_until_complete
self._check_closed()
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/base_events.py", line 480, in _check_closed
raise RuntimeError('Event loop is closed')
builtins.RuntimeError: Event loop is closed
Я искал несколько примеров переполнения стека, но для меня это не имеет смысла.