File tree Expand file tree Collapse file tree 2 files changed +30
-15
lines changed Expand file tree Collapse file tree 2 files changed +30
-15
lines changed Original file line number Diff line number Diff line change 1
1
from __future__ import annotations
2
2
3
3
import asyncio
4
+ import contextlib
4
5
import signal
5
6
from concurrent .futures import FIRST_EXCEPTION
6
7
from typing import TYPE_CHECKING
17
18
from .http import run_http_server
18
19
from .logging import init_logger
19
20
from .tgis_utils .args import EnvVarArgumentParser , add_tgis_args , postprocess_tgis_args
21
+ from .utils import check_for_failed_tasks
20
22
21
23
if TYPE_CHECKING :
22
24
import argparse
@@ -54,23 +56,16 @@ async def override_signal_handler() -> None:
54
56
55
57
tasks .append (loop .create_task (override_signal_handler ()))
56
58
57
- done , pending = await asyncio .wait (
58
- tasks ,
59
- return_when = FIRST_EXCEPTION ,
60
- )
61
- for task in pending :
62
- task .cancel ()
59
+ with contextlib .suppress (asyncio .CancelledError ):
60
+ await asyncio .wait (
61
+ tasks ,
62
+ return_when = FIRST_EXCEPTION ,
63
+ )
63
64
64
- while done :
65
- task = done .pop ()
66
- exc = task .exception ()
67
- if not exc :
68
- continue
69
-
70
- name = task .get_name ()
71
- coro_name = task .get_coro ().__name__
65
+ for task in tasks :
66
+ task .cancel ()
72
67
73
- raise RuntimeError ( f"task= { name } ( { coro_name } )" ) from exc
68
+ check_for_failed_tasks ( tasks )
74
69
75
70
76
71
if __name__ == "__main__" :
Original file line number Diff line number Diff line change
1
+ import asyncio
2
+ from collections .abc import Iterable
3
+
4
+
5
+ def check_for_failed_tasks (tasks : Iterable [asyncio .Task ]) -> None :
6
+ """Check a sequence of tasks exceptions and raise the exception."""
7
+ for task in tasks :
8
+ try :
9
+ exc = task .exception ()
10
+ except asyncio .InvalidStateError :
11
+ # no exception is set
12
+ continue
13
+
14
+ if not exc :
15
+ continue
16
+
17
+ name = task .get_name ()
18
+ coro_name = task .get_coro ().__name__
19
+
20
+ raise RuntimeError (f"task={ name } ({ coro_name } )" ) from exc
You can’t perform that action at this time.
0 commit comments