我需要在*.exe 2.7中并行运行不同的python文件。这些*.exe文件是使用C++语言创建的。另外,我需要为每个被调用的*.exe文件打开cmd窗口。
我已经尝试过一些多处理库,但它没有起作用。任何事情都会导致电脑崩溃..。
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窗口指示运行的代码的状态.
谢谢!
发布于 2019-10-02 18:36:23
Update<\b>:在Python2.7中,您可以:
pip install futures
获取concurrent.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)希望这能有所帮助
https://stackoverflow.com/questions/58207131
复制相似问题