ProcessPoolExecutor 和 Ctrl C
Posted
技术标签:
【中文标题】ProcessPoolExecutor 和 Ctrl C【英文标题】:ProcessPoolExectur and Ctrl C 【发布时间】:2021-10-24 09:08:35 【问题描述】:我在 Windows 10 上使用 ProcessPoolExecutor。Python 版本是 3.9.5。 当我按 Ctrl+C 两次时,即使我设置了超时,程序也会无休止地挂起。
with concurrent.futures.ProcessPoolExecutor() as executor:
results = executor.map(Worker, iterable, timeout=5)
try:
for result in results:
DoSomething(result)
except Exception as exc:
print(exc)
executor.shutdown(wait=True, cancel_futures=True)
错误信息是:
...
File "C:\Python\foo.py", line 162, in FooFunc
executor.shutdown(wait=True, cancel_futures=True)
File "C:\Python\_envs\Python39\lib\concurrent\futures\_base.py", line 636, in __exit__
self.shutdown(wait=True)
File "C:\Python\_envs\Python39\lib\concurrent\futures\process.py", line 740, in shutdown
self._executor_manager_thread.join()
File "C:\Python\_envs\Python39\lib\threading.py", line 1033, in join
self._wait_for_tstate_lock()
File "C:\Python\_envs\Python39\lib\threading.py", line 1049, in _wait_for_tstate_lock
elif lock.acquire(block, timeout):
KeyboardInterrupt
【问题讨论】:
【参考方案1】:我找到了解决方案。一种解决方法。这有点奇怪,因为我希望 Python 标准实现已经能够处理 Ctrl+C。
解决方法是添加到主进程中:
def handler(signum, frame):
print('Signal handler called with signal', signum)
if __name__ == "__main__":
import signal
signal.signal(signal.SIGINT, handler)
【讨论】:
以上是关于ProcessPoolExecutor 和 Ctrl C的主要内容,如果未能解决你的问题,请参考以下文章
为啥 ProcessPoolExecutor 和 Pool 在调用 super() 时会崩溃?
使用concurrent.futures和ProcessPoolExecutor来替代线程和进程
使用 Concurrent.Futures.ProcessPoolExecutor 运行同时和独立的 ABAQUS 模型
将带有对象的函数传递给 concurrent.futures.ProcessPoolExecutor()?
如何使用 asyncio 和 concurrent.futures.ProcessPoolExecutor 在 Python 中终止长时间运行的计算(CPU 绑定任务)?