Douglas
Douglas

Reputation: 1

How to run more than one executable (*.exe) at the same time (parallel) in python?

I need to run different *.exe files in parallel in python 2.7. These *.exe files were created using the C++ language. Also, I need that the cmd window opens for each *.exe file called.

I've already tried something of multiprocessing library, but it didn't work. Anything happened and the PC crashed...

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)

I have an intermediate knowledge of python and I don't know anything about parallelization in Python.

The code that I described does exactly that I need but in a sequential way. The parallelization that I described will decrease the code runtime in about 1h.

It is very important that the cmd window opens for each *.exe, because these cmd windows indicate the status of the code running...

Thank You!

Upvotes: 0

Views: 776

Answers (1)

Prayson W. Daniel
Prayson W. Daniel

Reputation: 15568

Update<\b>: In Python 2.7, you can do:

pip install futures

to get all the features in concurrent.futures

Still using Python 2.7? DoD is 2020. If you move to 3.2+ you can easily use concurrent.futures with almost the same code.

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)

Hope this helps

Upvotes: 2

Related Questions