首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >concurrent.futures ThreadPoolExecutor无法等待?

concurrent.futures ThreadPoolExecutor无法等待?
EN

Stack Overflow用户
提问于 2012-05-14 21:29:21
回答 1查看 3.2K关注 0票数 2

我正在尝试使用concurrent.futures的ThreadPoolExecutor来提高脚本性能。我正在通过Popen启动一些外部python脚本,并将它们封装为未来的对象,但这些对象在完成后进入回调函数,但我可以看到它们在我的机器上运行(它们运行了相当长的时间)。代码如下所示:

代码语言:javascript
复制
with futures.ThreadPoolExecutor(max_workers=4) as executor: 
        p1 = executor.submit(subprocess.Popen([myotherPythonscript1], stdout = subprocess.PIPE))
        p1.add_done_callback(process_result)
        p2 = executor.submit(subprocess.Popen([myotherPythonscript2], stdout = subprocess.PIPE))
        p2.add_done_callback(process_result)

def process_result( future ):
        logger.info( "Seeding process finished...")

我还尝试了使用but ()和wait()未来函数的不同方法,但结果相同。未来的对象被标记为已完成,但实际上它们仍在运行。我是不是遗漏了什么?

谢谢,

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-14 21:36:48

你不能仅仅将Popen的结果传递给你的executor,你必须传递一个callable。

代码语言:javascript
复制
>>> from concurrent.futures import *
>>> from subprocess import *
>>> executor = ThreadPoolExecutor(4)
>>> future = executor.submit(Popen(['echo', 'test'], stdout=PIPE))
>>> future.exception()
TypeError("'Popen' object is not callable",)

从另一方面来说,这是可行的:

代码语言:javascript
复制
from concurrent.futures import *
from subprocess import *

def make_call():
    process = Popen(['echo', 'test'], stdout=PIPE)
    return process.communicate()[0]

executor = ThreadPoolExecutor(4)
future = executor.submit(make_call)
print(future.result())
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10584290

复制
相关文章

相似问题

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