python - 如何停止 multiprocessing.Pool.map 异常
问题描述
当我在 my 中引发异常时thread_function
,它不会停止其余的map
处理。我想阻止它。
def thread_function(n):
if n == 10:
raise Exception('Stop everything!')
pool = Pool(processes = 4)
pool.map(thread_function, range(1, 1000), chunksize = 1)
我希望在一个线程到达后不再进行处理n == 10
。
解决方案
我不知道直接使用的方法,map
但是您可以async_map
像这样监视...
from multiprocessing import Pool
import time
def thread_function(n):
if n == 10:
print('Raising Exception')
raise Exception('Stop everything!')
print(n)
time.sleep(0.1)
pool = Pool(processes = 4)
result = pool.map_async(thread_function, range(1, 1000), chunksize = 1)
while not result.ready():
if not result._success:
print('Exiting for failure')
pool.terminate()
pool.join()
break
推荐阅读
- linux - SonarQube 无法启动网络
- c# - 根据查找表为数组元素赋值
- azure - 从 Azure Function 调用依赖项时,此平台不支持 EventLog 访问
- reactjs - 如何在 react-router v3 的 URL 中传递/注入“文本”?
- rust - Rust 通过空格分割行:没有为类型 &str 找到方法收集
- apache-kafka - 将 kafka-connect-transform-archive 与 HdfsSinkConnector 一起使用时的刷新大小
- powerbi - 需要建议来设计我的 Azure 分析服务数据模型
- android - Gradle 无法生成 android 包
- javascript - 客户端重定向时如何取消浏览器XHR多个回调队列?
- facebook - Facebook Webhooks 页面:获得喜欢和不喜欢