I've read here here and other places that, as a rule of thumb asyncio.CancelledError should be reraised. Is this simply to avoid problems shutdown down, or are there other reasons?
Suppose I wanted to raise an exception when a worker monitoring a long test is canceled. I want to raise an exception only at the end of the test, at shutdown). Is it generally safe to catch CancelledError and re-raise a different exception?
@astask
async def monitor():
ex = None
try:
while True:
ex_ = await check()
if ex is None and ex_ is not None:
ex = ex_
logger.error("Failed. Let's see it fails more...")
except:
if ex: raise ex
raise
@astask
async def test():
... run the test
async def test():
tasks = [monitor(), test()]
await awaitFirstTaskDoneThaenCancelAllThenRaiseFirstTaskException(tasks)