首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ProcessPoolExectur和Ctrl C

ProcessPoolExectur和Ctrl C
EN

Stack Overflow用户
提问于 2021-08-24 05:53:05
回答 1查看 71关注 0票数 1

我在Windows10上使用ProcessPoolExecutor。Python版本是3.9.5。当我按Ctrl+C两次时,即使我设置了超时,程序也会无休止地挂起。

代码语言:javascript
复制
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)

错误消息为:

代码语言:javascript
复制
  ...
  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
EN

回答 1

Stack Overflow用户

发布于 2021-08-24 10:23:05

我找到了一个解决方案。一种解决方法。这有点奇怪,因为我认为Python标准实现已经能够处理Ctrl+C了。

解决方案是在主进程中添加:

代码语言:javascript
复制
def handler(signum, frame):
    print('Signal handler called with signal', signum)


if __name__ == "__main__":
    import signal
    signal.signal(signal.SIGINT, handler)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68902374

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档