我需要在*.exe
2.7中并行运行不同的python
文件。这些*.exe
文件是使用C++
语言创建的。另外,我需要为每个调用的*.exe
文件打开cmd窗口。
我已经尝试了多处理库,但是没有用。发生任何事情,PC崩溃了...
import os
import subprocess
cwd = os.getcwd()
cmd_MakeBackgroundImage = cwd+"\\MakeBackgroundImage.exe"
cmd_SFVTest = cwd + "\\SFVTest.exe"
cmd_Wavelet_x = cwd + "\\Wavelet_x.exe"
cmd_Dataform_x = cwd + "\\Dataform_x.exe"
cmd_Wavelet_y = cwd + "\\Wavelet_y.exe"
cmd_Dataform_y = cwd + "\\Dataform_y.exe"
process_1 = os.system(cmd_MakeBackgroundImage)
process_2 = os.system(cmd_SFVTest)
#I need to run "cmd_Wavelet_x" at the same time of "cmd_Wavelet_y"
#So I need to parallelize process_3 and process_4
process_3 = os.system(cmd_Wavelet_x)
process_4 = os.system(cmd_Wavelet_y)
#I need to run "cmd_Dataform_x" at the same time of "cmd_Dataform_y"
#So I need to parallelize process_5 and process_6
process_5 = os.system(cmd_Dataform_x)
process_6 = os.system(cmd_Dataform_y)
我对python有一定的了解,而我对Python的并行化一无所知。
我描述的代码确实可以满足我的需要,但是顺序执行 办法。我描述的并行化将减少代码 运行时间约1小时。
为每个*.exe
打开cmd窗口非常重要,因为这些cmd窗口指示运行代码的状态...
谢谢!
答案 0 :(得分:1)
更新<\ b>:在Python 2.7中,您可以执行以下操作:
pip install futures
获得并发的所有功能。
仍然使用Python 2.7吗? DoD是2020年。如果您升级到3.2+,则可以轻松使用concurrent.futures
并使用几乎相同的代码。
import concurrent.futures
import os
cwd = os.getcwd()
cmd_MakeBackgroundImage = cwd+"\\MakeBackgroundImage.exe"
cmd_SFVTest = cwd + "\\SFVTest.exe"
cmd_Wavelet_x = cwd + "\\Wavelet_x.exe"
cmd_Dataform_x = cwd + "\\Dataform_x.exe"
cmd_Wavelet_y = cwd + "\\Wavelet_y.exe"
cmd_Dataform_y = cwd + "\\Dataform_y.exe"
# add cmds to be ran concurrently
programs = [cmd_SFVTest, cmd_Wavelet_x, cmd_Dataform_x, cmd_Wavelet_y, cmd_Dataform_y]
# you can use either Process or Thread depends on your needs
with concurrent.futures.ProcessPoolExecutor() as executor:
executor.map(os.system, programs)
希望这会有所帮助