|
25 | 25 | import queue |
26 | 26 | from typing import Any, List, Optional, Set, cast |
27 | 27 |
|
| 28 | +_TASK_AWAIT_TIMEOUT = 1 |
| 29 | +_GET_ALL_TASKS_TIMEOUT = 1 |
| 30 | +_WAIT_FOR_LOOP_TASKS_TIMEOUT = 2 # Must be larger than _TASK_AWAIT_TIMEOUT |
| 31 | + |
28 | 32 |
|
29 | 33 | def get_best_available_queue() -> queue.Queue: |
30 | 34 | """Create the best available queue type.""" |
@@ -73,16 +77,19 @@ async def _async_get_all_tasks(loop: asyncio.AbstractEventLoop) -> List[asyncio. |
73 | 77 |
|
74 | 78 | async def _wait_for_loop_tasks(wait_tasks: Set[asyncio.Task]) -> None: |
75 | 79 | """Wait for the event loop thread we started to shutdown.""" |
76 | | - await asyncio.wait(wait_tasks, timeout=1) |
| 80 | + await asyncio.wait(wait_tasks, timeout=_TASK_AWAIT_TIMEOUT) |
77 | 81 |
|
78 | 82 |
|
79 | 83 | def shutdown_loop(loop: asyncio.AbstractEventLoop) -> None: |
80 | 84 | """Wait for pending tasks and stop an event loop.""" |
81 | | - pending_tasks = set(asyncio.run_coroutine_threadsafe(_async_get_all_tasks(loop), loop).result()) |
82 | | - done_tasks = set(task for task in pending_tasks if not task.done()) |
83 | | - pending_tasks -= done_tasks |
| 85 | + pending_tasks = set( |
| 86 | + asyncio.run_coroutine_threadsafe(_async_get_all_tasks(loop), loop).result(_GET_ALL_TASKS_TIMEOUT) |
| 87 | + ) |
| 88 | + pending_tasks -= set(task for task in pending_tasks if task.done()) |
84 | 89 | if pending_tasks: |
85 | | - asyncio.run_coroutine_threadsafe(_wait_for_loop_tasks(pending_tasks), loop).result() |
| 90 | + asyncio.run_coroutine_threadsafe(_wait_for_loop_tasks(pending_tasks), loop).result( |
| 91 | + _WAIT_FOR_LOOP_TASKS_TIMEOUT |
| 92 | + ) |
86 | 93 | loop.call_soon_threadsafe(loop.stop) |
87 | 94 |
|
88 | 95 |
|
|
0 commit comments