客户的服务器卡在 3.9,所以没有 TaskGroup,我对两个命名任务用了 gather。我在文档里读到一个抛异常时其他的会被取消,对吧?pier-9 的 measurements 接口今天返回 503,打印了 "score failed",但 weights 还活着,然后照样写了 weights_cache.json。main.py 里那个半秒的 sleep 只是为了让最后的检查排在写入之后。
async def score(pier):
m = asyncio.create_task(fetch_measurements(pier), name="measurements")
w = asyncio.create_task(fetch_weights(pier), name="weights")
measurements, weights = await asyncio.gather(m, w)
return combine(measurements, weights)
$ python3 main.py
score failed: measurements: upstream 503 for pier-9
in flight after failure: ['weights']
weights written to weights_cache.json for pier-9
in flight at end: []
为什么失败之后 weights 还在跑?
从英语翻译·查看原文The client's server is stuck on 3.9, so no TaskGroup, and I used gather on two named tasks. I read in the docs that when one raises, the others get cancelled, right? pier-9's measurements endpoint is 503ing today, "score failed" prints, but weights is still alive and then writes weights_cache.json anyway. The half second sleep in main.py is only there so the last check comes after the write.
async def score(pier):
m = asyncio.create_task(fetch_measurements(pier), name="measurements")
w = asyncio.create_task(fetch_weights(pier), name="weights")
measurements, weights = await asyncio.gather(m, w)
return combine(measurements, weights)
$ python3 main.py
score failed: measurements: upstream 503 for pier-9
in flight after failure: ['weights']
weights written to weights_cache.json for pier-9
in flight at end: []
Why is weights still running after the failure?