【发布时间】:2018-07-19 17:07:58
【问题描述】:
您好,我的异步循环有以下内容
async def start_process(restore_items, args, loop):
with GlacierRestorer(args.temp_dir, args.error_log_bucket, loop) as restorer:
restorer.initiate_restore_all(restore_items)
tasks = []
semaphore = asyncio.BoundedSemaphore(4)
for item in restore_items:
tasks.append(asyncio.ensure_future(restorer.transfer(item, semaphore)))
await asyncio.gather(*tasks)
def main():
args = get_args()
restore_items = get_restore_items(args)
for item in restore_items:
print(item.source, ':', item.destination)
try:
loop = asyncio.get_event_loop()
loop.run_until_complete(start_process(restore_items, args, loop))
except KeyboardInterrupt:
pass
我的工作和文件变大了 socket.send() 异常 阅读文档后,它似乎来自 loop.run_until_complete
异常不会导致程序崩溃,但最终会使程序陷入困境以至于无法打印异常。
如何修改当前代码来解决这个问题?
【问题讨论】:
-
run_until_complete只传播由start_process协程引发的异常。异常可能发生在transfer协程之一中,gather尽职尽责地报告它。如果要忽略异常,可以将restorer.transfer包装在单独的async def that only doestry/await restorer.transfer(...)/except SocketError: pass`中。 -
@user4815162342 我对你在sn-p代码中解释的反应头脑有点困惑?
-
我现在已将评论扩展为显示代码的答案。它不完整,因为我不了解异常的原因,但它显示了如果在您的情况下可以忽略它。
-
@user4815162342 介意我再打扰你一下吗?
-
如果您有新问题,请考虑提出单独的问题。如果可能,请包含一个可用于重现问题的最小示例。
标签: python-3.x asynchronous boto3 python-asyncio