没有详细信息的Python异步CancelledError()

时间:2019-05-08 12:41:35

标签: python-3.x python-asyncio

以下代码失败,我无法获得实际错误,我仅收到大量CancelledError消息

import aiobotocore, asyncio

async def replicate_to_region(chunks, region):
    session = aiobotocore.get_session()
    client = session.create_client('dynamodb', region_name=region)
    start = 0
    while True:
        chunk = chunks[start]
        item = {'my_table': chunk}
        response = await client.batch_write_item(RequestItems=item)

async def main():
    asyncio.gather(*(replicate_to_region(payloads, region) for region in regions))

asyncio.run(main())

我收到以下错误;

client_session: <aiohttp.client.ClientSession object at 0x7f6fb65a34a8>
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x7f6fb64c82b0>
_GatheringFuture exception was never retrieved
future: <_GatheringFuture finished exception=CancelledError()>
concurrent.futures._base.CancelledError
_GatheringFuture exception was never retrieved
future: <_GatheringFuture finished exception=CancelledError()>

我已经尝试了replicate_to_region函数的许多变体,但是它们都因上述相同错误而失败。能够看到实际错误是很有用的。

1 个答案:

答案 0 :(得分:1)

async def main():
    asyncio.gather(...)

asyncio.gather()本身是可以期待的:

  

等待 asyncio.gather(* aws,循环=无,return_exceptions = False)

这意味着您在处理它时应该使用await:

async def main():
    await asyncio.gather(*(replicate_to_region(payloads, region) for region in regions))

题外话:

我没有使用aiobotocore,也不确定它是否重要,但是最好按照文档所述进行操作。特别是,在创建客户端as example shows时,您可能应该使用async with