使用 Concurrent.Futures 模块作为 ThreadPool 调用 Python-get 函数的返回值

标签 python multithreading return-value concurrent.futures

我使用 Concurrent.Futures 模块创建了一个 ThreadPool。
我创建了线程,一切正常。

但现在我有一个函数,我想让它与线程并行,
问题是我需要每个线程应该运行的函数的返回值,并且不确定在使用 future 线程池模块时如何获取它。
有办法吗?

最佳答案

您对 result() 的理解是绝对错误的。来自 doc :

result(timeout=None)
Return the value returned by the call. If the call hasn’t yet completed then this method will wait up to timeout seconds. If the call hasn’t completed in timeout seconds, then a TimeoutError will be raised. timeout can be an int or float. If timeout is not specified or None, there is no limit to the wait time.

If the future is cancelled before completing then CancelledError will be raised.



很明显result()将为您提供您提交的该功能的结果。

关于使用 Concurrent.Futures 模块作为 ThreadPool 调用 Python-get 函数的返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32224933/

相关文章:

python - Django-cms 无法按照教程创建项目创建项目发生失败

python - 如何使用集群中的多个节点处理海量数据——python

python - Keras 自定义层和 Eager Execution

c - 打印返回结构的成员

python - R 翻译成 Python

c - 如何检查内存是否可用于读/写操作?

objective-c - Objective-C NSThread 可以访问全局变量吗?

Java多线程: a good way to notify the consumer threads that all the producer threads have finished?

c++ - 在引用 C++ 中存储函数的返回值

perl - 有没有办法让 screen 返回 session id 或 pid?