|
| 1 | +import asyncio |
| 2 | +import itertools |
| 3 | +import logging |
| 4 | +import threading |
| 5 | + |
| 6 | +# pylint: disable=invalid-name |
| 7 | +# pylint: disable=global-statement |
| 8 | +try: |
| 9 | + # Python 3.8 or newer has a suitable process watcher |
| 10 | + asyncio.ThreadedChildWatcher |
| 11 | +except AttributeError: |
| 12 | + # backport the Python 3.8 threaded child watcher |
| 13 | + import os |
| 14 | + import warnings |
| 15 | + |
| 16 | + # Python 3.7 preferred API |
| 17 | + _get_running_loop = getattr(asyncio, "get_running_loop", asyncio.get_event_loop) |
| 18 | + |
| 19 | + class _Py38ThreadedChildWatcher(asyncio.AbstractChildWatcher): |
| 20 | + def __init__(self): |
| 21 | + self._pid_counter = itertools.count(0) |
| 22 | + self._threads = {} |
| 23 | + |
| 24 | + def is_active(self): |
| 25 | + return True |
| 26 | + |
| 27 | + def close(self): |
| 28 | + pass |
| 29 | + |
| 30 | + def __enter__(self): |
| 31 | + return self |
| 32 | + |
| 33 | + def __exit__(self, exc_type, exc_val, exc_tb): |
| 34 | + pass |
| 35 | + |
| 36 | + def __del__(self, _warn=warnings.warn): |
| 37 | + threads = [t for t in list(self._threads.values()) if t.is_alive()] |
| 38 | + if threads: |
| 39 | + _warn( |
| 40 | + f"{self.__class__} has registered but not finished child processes", |
| 41 | + ResourceWarning, |
| 42 | + source=self, |
| 43 | + ) |
| 44 | + |
| 45 | + def add_child_handler(self, pid, callback, *args): |
| 46 | + loop = _get_running_loop() |
| 47 | + thread = threading.Thread( |
| 48 | + target=self._do_waitpid, |
| 49 | + name=f"waitpid-{next(self._pid_counter)}", |
| 50 | + args=(loop, pid, callback, args), |
| 51 | + daemon=True, |
| 52 | + ) |
| 53 | + self._threads[pid] = thread |
| 54 | + thread.start() |
| 55 | + |
| 56 | + def remove_child_handler(self, pid): |
| 57 | + # asyncio never calls remove_child_handler() !!! |
| 58 | + # The method is no-op but is implemented because |
| 59 | + # abstract base class requires it |
| 60 | + return True |
| 61 | + |
| 62 | + def attach_loop(self, loop): |
| 63 | + pass |
| 64 | + |
| 65 | + def _do_waitpid(self, loop, expected_pid, callback, args): |
| 66 | + assert expected_pid > 0 |
| 67 | + |
| 68 | + try: |
| 69 | + pid, status = os.waitpid(expected_pid, 0) |
| 70 | + except ChildProcessError: |
| 71 | + # The child process is already reaped |
| 72 | + # (may happen if waitpid() is called elsewhere). |
| 73 | + pid = expected_pid |
| 74 | + returncode = 255 |
| 75 | + logger.warning( |
| 76 | + "Unknown child process pid %d, will report returncode 255", pid |
| 77 | + ) |
| 78 | + else: |
| 79 | + if os.WIFSIGNALED(status): |
| 80 | + returncode = -os.WTERMSIG(status) |
| 81 | + elif os.WIFEXITED(status): |
| 82 | + returncode = os.WEXITSTATUS(status) |
| 83 | + else: |
| 84 | + returncode = status |
| 85 | + |
| 86 | + if loop.get_debug(): |
| 87 | + logger.debug( |
| 88 | + "process %s exited with returncode %s", expected_pid, returncode |
| 89 | + ) |
| 90 | + |
| 91 | + if loop.is_closed(): |
| 92 | + logger.warning("Loop %r that handles pid %r is closed", loop, pid) |
| 93 | + else: |
| 94 | + loop.call_soon_threadsafe(callback, pid, returncode, *args) |
| 95 | + |
| 96 | + self._threads.pop(expected_pid) |
| 97 | + |
| 98 | + # add the watcher to the loop policy |
| 99 | + asyncio.get_event_loop_policy().set_child_watcher(_Py38ThreadedChildWatcher()) |
| 100 | + |
| 101 | +__all__ = ["EventLoopThread", "get_event_loop", "stop_event_loop", "run_coroutine"] |
| 102 | + |
| 103 | +logger = logging.getLogger(__name__) |
| 104 | + |
| 105 | +class EventLoopThread(threading.Thread): |
| 106 | + loop = None |
| 107 | + _count = itertools.count(0) |
| 108 | + |
| 109 | + def __init__(self): |
| 110 | + name = f"{type(self).__name__}-{next(self._count)}" |
| 111 | + super().__init__(name=name, daemon=True) |
| 112 | + |
| 113 | + def __repr__(self): |
| 114 | + loop, r, c, d = self.loop, False, True, False |
| 115 | + if loop is not None: |
| 116 | + r, c, d = loop.is_running(), loop.is_closed(), loop.get_debug() |
| 117 | + return ( |
| 118 | + f"<{type(self).__name__} {self.name} id={self.ident} " |
| 119 | + f"running={r} closed={c} debug={d}>" |
| 120 | + ) |
| 121 | + |
| 122 | + def run(self): |
| 123 | + self.loop = loop = asyncio.new_event_loop() |
| 124 | + asyncio.set_event_loop(loop) |
| 125 | + |
| 126 | + try: |
| 127 | + loop.run_forever() |
| 128 | + finally: |
| 129 | + try: |
| 130 | + shutdown_asyncgens = loop.shutdown_asyncgens() |
| 131 | + except AttributeError: |
| 132 | + pass |
| 133 | + else: |
| 134 | + loop.run_until_complete(shutdown_asyncgens) |
| 135 | + loop.close() |
| 136 | + asyncio.set_event_loop(None) |
| 137 | + |
| 138 | + def stop(self): |
| 139 | + loop, self.loop = self.loop, None |
| 140 | + if loop is None: |
| 141 | + return |
| 142 | + loop.call_soon_threadsafe(loop.stop) |
| 143 | + self.join() |
| 144 | + |
| 145 | +_lock = threading.Lock() |
| 146 | +_loop_thread = None |
| 147 | + |
| 148 | +def get_event_loop(): |
| 149 | + global _loop_thread |
| 150 | + with _lock: |
| 151 | + if _loop_thread is None: |
| 152 | + _loop_thread = EventLoopThread() |
| 153 | + _loop_thread.start() |
| 154 | + return _loop_thread.loop |
| 155 | + |
| 156 | +def stop_event_loop(): |
| 157 | + global _loop_thread |
| 158 | + with _lock: |
| 159 | + if _loop_thread is not None: |
| 160 | + _loop_thread.stop() |
| 161 | + _loop_thread = None |
| 162 | + |
| 163 | +def run_coroutine(coro): |
| 164 | + return asyncio.run_coroutine_threadsafe(coro, get_event_loop()) |
0 commit comments