gpt4 book ai didi

python-3.x - 没有细节的 Python async CancelledError()

转载 作者:行者123 更新时间:2023-12-01 21:57:09 31 4
gpt4 key购买 nike

下面的代码失败了,我无法得到实际的错误,我只是收到许多 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 函数的变体,但它们都失败了,并出现了上述相同的错误。如果能够看到实际错误是什么,这将很有用。

最佳答案

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

asyncio.gather()是一个等待本身:

awaitable asyncio.gather(*aws, loop=None, return_exceptions=False)

这意味着你应该在处理它时使用等待:

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

题外话:

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

关于python-3.x - 没有细节的 Python async CancelledError(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56041069/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com