- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在本文档 ( https://pymotw.com/3/concurrent.futures/ ) 中说:
“ProcessPoolExecutor 的工作方式与 ThreadPoolExecutor 相同,但使用进程而不是线程。这允许 CPU 密集型操作使用单独的 CPU,而不会被 CPython 解释器的全局解释器锁阻塞。”
这听起来不错!它还说:
“如果其中一个工作进程发生意外导致其意外退出,则 ProcessPoolExecutor 被视为“已损坏”并且将不再安排任务。”
这听起来很糟糕 :( 所以我想我的问题是:什么被认为是“意外”?这是否仅仅意味着退出信号不是 1?我可以安全地退出线程并继续处理队列吗?示例如下如下:
from concurrent import futures
import os
import signal
with futures.ProcessPoolExecutor(max_workers=2) as ex:
print('getting the pid for one worker')
f1 = ex.submit(os.getpid)
pid1 = f1.result()
print('killing process {}'.format(pid1))
os.kill(pid1, signal.SIGHUP)
print('submitting another task')
f2 = ex.submit(os.getpid)
try:
pid2 = f2.result()
except futures.process.BrokenProcessPool as e:
print('could not start new tasks: {}'.format(e))
最佳答案
我没有在 IRL 中看到它,但从代码来看,返回的文件描述符似乎不包含 results_queue 文件描述符。
来自 concurrent.futures.process:
reader = result_queue._reader
while True:
_add_call_item_to_queue(pending_work_items,
work_ids_queue,
call_queue)
sentinels = [p.sentinel for p in processes.values()]
assert sentinels
ready = wait([reader] + sentinels)
if reader in ready: # <===================================== THIS
result_item = reader.recv()
else:
# Mark the process pool broken so that submits fail right now.
executor = executor_reference()
if executor is not None:
executor._broken = True
executor._shutdown_thread = True
executor = None
# All futures in flight must be marked failed
for work_id, work_item in pending_work_items.items():
work_item.future.set_exception(
BrokenProcessPool(
"A process in the process pool was "
"terminated abruptly while the future was "
"running or pending."
))
# Delete references to object. See issue16284
del work_item
wait
函数取决于系统,但假设 linux 操作系统(在 multiprocessing.connection
处,删除了所有与超时相关的代码):
def wait(object_list, timeout=None):
'''
Wait till an object in object_list is ready/readable.
Returns list of those objects in object_list which are ready/readable.
'''
with _WaitSelector() as selector:
for obj in object_list:
selector.register(obj, selectors.EVENT_READ)
while True:
ready = selector.select(timeout)
if ready:
return [key.fileobj for (key, events) in ready]
else:
# some timeout code
关于python - ProcessPoolExecutor,BrokenProcessPool处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52617401/
我正在尝试学习 python 中多处理的基础知识,并在网上找到了以下我想练习的示例。 import concurrent.futures import time def do_something(se
在创建我需要的实际应用程序之前,我试图对此有一个基本的了解。我最近从 2.7 转移到了 3.3。 this code from the python docs 的直接复制粘贴失败,来自 here 的一
使用 concurrent.futures.ProcessPoolExecutor 我正在尝试运行第一段代码以并行执行函数“Calculate_Forex_Data_Derivatives(data,
在 sklearn.model_selection.cross_val_score 中使用 n_jobs = -1 作为参数时出现错误。我是深度学习和 ANN 的初学者,根据 this 中的讲师在 k
我正在我的 Linux 机器上安装 anaconda。我按照这里的说明进行操作。 https://docs.anaconda.com/anaconda/install/linux/ 但是,在选择安装位
一言以蔽之 当使用 concurrent.futures 并行化我的代码时,我得到了一个 BrokenProcessPool 异常。不会显示更多错误。我想找到错误的原因并询问如何做到这一点的想法。 完
我在 Windows 10 上使用 Python 3.5.3 运行 Jupyter 5.0.0 notebook。以下示例代码无法运行: from concurrent.futures import
我是一名优秀的程序员,十分优秀!