![](/img/trans.png)
[英]Problem with concurrent.futures ThreadPoolExecutor in python
[英]concurrent.futures ThreadPoolExecutor fails to wait?
我試圖通過並發使用ThreadPoolExecutor來提高腳本性能。 我通過Popen啟動了一些外部python腳本,並將它們封裝為將來的對象,但是這些對象完成后進入了回調函數,但是我可以看到它們在我的機器上運行(它們運行了相當長的時間)。 代碼如下:
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...")
我還嘗試過使用running()和wait()將來的函數使用不同的方法,但是結果相同。 將來的對象被標記為已完成,但實際上它們仍在運行。 我想念什么嗎?
謝謝,
您不能只是將Popen
的結果傳遞給執行程序,而必須傳遞一個callable。
>>> 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",)
另一方面,它可以工作:
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())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.