Kill the task and optionally terminate its thread.
(self, terminate_thread: bool = False)
| 145 | return await loop.run_in_executor(None, _get_result) |
| 146 | |
| 147 | def kill(self, terminate_thread: bool = False) -> None: |
| 148 | """Kill the task and optionally terminate its thread.""" |
| 149 | self.kill_children() |
| 150 | if self._future and not self._future.done(): |
| 151 | self._future.cancel() |
| 152 | |
| 153 | if terminate_thread and self.event_loop_thread.loop: |
| 154 | if self.event_loop_thread.loop.is_running(): |
| 155 | try: |
| 156 | cleanup_future = asyncio.run_coroutine_threadsafe( |
| 157 | self._drain_event_loop_tasks(), self.event_loop_thread.loop |
| 158 | ) |
| 159 | cleanup_future.result() |
| 160 | except Exception: |
| 161 | pass |
| 162 | |
| 163 | self.event_loop_thread.terminate() |
| 164 | |
| 165 | def kill_children(self) -> None: |
| 166 | for child in self.children: |
no test coverage detected