def _terminate_pool(_task_queue: SimpleQueue, _in_queue: SimpleQueue, out_queue: SimpleQueue, pool: List[Process], change_notifier: SimpleQueue, worker_handler_th: Thread, handle_task_th: Thread, handle_result_th: Thread): """ 终止进程池 :param _task_queue: 暂不使用 :param _in_queue: 暂不使用 :param out_queue: 通知结束进程 :param pool: 进程池 :param change_notifier: 通知状态改变 :param worker_handler_th: worker管理进程 :param handle_task_th: 任务管理进程 :param handle_result_th: 执行结果管理进程 :return: """ worker_handler_th._state = State.TERMINATE handle_task_th._state = State.TERMINATE assert handle_result_th.is_alive(), "result handler not alive" handle_result_th._state = State.TERMINATE # 发送终止信号 change_notifier.put(EndSignal.END) out_queue.put(EndSignal.END) # 等待检测进程的线程退出 if threading.current_thread() != worker_handler_th: worker_handler_th.join() # 向进程池中的所有进程发送终止信号 if pool: for p in pool: if p.exitcode is None: p.terminate() # 等待任务处理线程退出 if threading.current_thread() != handle_task_th: handle_task_th.join() # 等待处理结果线程退出 if threading.current_thread() != handle_result_th: handle_result_th.join() # 等待所有存活的进程退出 if pool: for p in pool: if p.is_alive(): p.join()
def _set_daemon_and_start(th: Thread): """ 设置成守护线程后运行 :param th: 运行的进程 :return: """ th.setDaemon(True) th._state = State.RUN th.start()
def _terminate_pool(cls, pool: 'Pool', worker_handler: threading.Thread) -> None: worker_handler._state = TERMINATE # We must wait for the worker handler to exit before terminating # workers because we don't want workers to be restarted behind our back. if threading.current_thread() is not worker_handler: worker_handler.join() if pool and hasattr(pool[0], 'terminate'): for p in pool: if p.exitcode is None: p.terminate() # Join pool workers if pool and hasattr(pool[0], 'terminate'): for p in pool: if p.is_alive(): # worker has not yet exited p.join()