Python异步问题

2024-04-30 14:03:28 发布

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

我有很多机器运行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

我搜索了一些关于堆栈溢出的例子,但对我来说没有意义。你知道吗


Tags: inpyloopasynciovenvliblinesite