- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我已经给 concurrent.futures.ThreadPoolExecutor
一堆任务,我想等到它们都完成后再继续流程。我怎样才能做到这一点,而不必保存所有 future 并对其调用 wait
? (我想对执行者采取行动。)
最佳答案
只需调用 Executor.shutdown
:
shutdown(wait=True)
Signal the executor that it should free any resources that it is using when the currently pending futures are done executing. Calls to
Executor.submit()
andExecutor.map()
made after shutdown will raiseRuntimeError
.If wait is
True
then this method will not return until all the pending futures are done executing and the resources associated with the executor have been freed.
但是,如果您在列表中跟踪您的 future ,则可以使用 futures.wait()
避免关闭执行程序以备将来使用。功能:
concurrent.futures.wait(fs, timeout=None, return_when=ALL_COMPLETED)
Wait for the
Future
instances (possibly created by differentExecutor
instances) given byfs
to complete. Returns a named 2-tuple of sets. The first set, named done, contains the futures that completed (finished or were cancelled) before the wait completed. The second set, named not_done, contains uncompleted futures.
请注意,如果您不提供 timeout
,它会等到所有 future 都完成。
您也可以使用futures.as_completed()
相反,但是您必须对其进行迭代。
关于Python:WAITING所有 `concurrent.futures.ThreadPoolExecutor` 的 future ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21143162/
这是正确的吗? ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor)
我只是对我编写的一些代码感到非常困惑。我惊讶地发现: with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
我正在尝试同时使用 InheritableThreadLocal 和 ThreadPoolExecutor。 这是因为 ThreadPoolExecutor 为每个池重用线程(毕竟它是一个池),这意味
concurrent.futures.ThreadPoolExecutor 通过 传递 function 到执行器 executor.submit(my_function) 像这样: def my_f
将一个简单的测试程序放在一起,应该并行执行一些任务。每次我们提交6个任务,等待完成。然后,又提交了一组任务。 import java.util.concurrent.*; public class
我正在运行一段 python 代码,其中多个线程通过线程池执行程序运行。每个线程都应该执行一项任务(例如获取网页)。我想要做的是终止所有线程,即使其中一个线程失败。例如: with ThreadPoo
我有一个使用阻塞队列的ThreadPoolExecutor,并且正在尝试调试一个问题,在该问题中,我怀疑任务在ThreadPoolExecutor的队列中停留的时间太长,无法执行。我正在尝试验证这一理
我正在尝试使用 futures backport 包在 Python 中使用 ThreadPoolExecutor。然而,问题是所有线程都是同时执行的,所以没有实际的池化发生。更具体地说,我得到了该函
我有两个 list : a = [1, 2, 3, 4] b = [9, 8, 7, 6] 我希望将这两个列表的每个组合作为参数传递给我正在执行多线程处理的函数: def test(hello, wo
当我们谈论ThreadPoolExecutor时,核心池大小和最大池大小之间到底有什么区别? 可以用例子来解释吗? 最佳答案 来自this blog post : Take this example.
我对“concurrent.futures”的并行处理相当陌生,我正在测试一些简单的实验。我编写的代码似乎可以工作,但我不确定如何存储结果。我尝试创建一个列表(“ future ”)并将结果附加到该列
我审查了多线程,并尝试实现一个创建单独线程来运行收集进程的应用程序。该过程中的主要方法需要一个变量数组列表,我正在尝试找出一种将数组列表传递给每个线程的方法。 ApplicationContext c
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我需要读取一个大的 csv 文件(328 MB)并对其进行处理。每行的处理还包括调用 Web 服务。 我是第一次使用ThreadPoolExecutor。我的逻辑是,我将从 csv 中每 100 行吐
from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED def div_zero(x): print('I
我有一个带有 run 方法的类,该类的 main 方法中的计时器正在使用以下代码调用该类: Timer timer = new Timer(); timer.scheduleAtFixedRate(n
尝试调试竞争条件,其中我们的应用程序的轮询器线程之一永远不会返回,导致 future 的轮询器永远不会被调度。用抽象术语来说,在捕获问题时隐藏我们的业务逻辑,这就是我们的代码路径。 我们必须更新远程服
下面的错误是什么意思?我怎样才能恢复它? Exception in thread "UserActionProcessor-8811" java.util.concurrent.RejectedExe
我正在致力于增强现有的 Java 应用程序。该应用程序是一个消息处理器,每天处理数百万条消息。它基本上是使用 Core Java 编写的,线程和队列是使用 Collection 类实现的。 在此应用程
我想更新使用用户定义线程池的旧代码。我想使用 java ThreadPoolExecutor,但问题是发送到线程池的请求不是可运行的。有什么方法可以将 ThreadPoolExecutor 与现有请求
我是一名优秀的程序员,十分优秀!