1
votes

I am a newbie in Python. I recently tried to use Python script to call an console exe which is a process need long time. I will allow the exe being called as many times as the CPU can permit. And when the exe has finish its job. The CPU can be allocated to other new jobs. So I think I may need the multiple process control mechanism.

Before I switch to multiprocessing.Process I can call my exe from subprocess.Popen. It works fine. However, after I switched to multiprocessing.Process. It seems start() can not call my exe (I can tell the difference from CPU performance). Can anyone give a clue on this? FYI, I am using the following code.

previously using subrpocess:

cmd = exeFileName + r" " + inputArguments

a = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

Now using multipleprocess:

b = multiprocessing.Process(target=exeFileName,args=inputArguments)

b.start()

As you may noticed, my exe need input arguments to work. So in the subprocess code, I use a string of cmd to capture the whole line. In multiprocessing code, I am not. It already has a target and args built-in. Thank you for your time!

1
Got it work finally. I discard multiprocessing at the end. Instead, I am using subprocess and its own poll() method to tell me whether the exe has terminated or not. Then append them to thread. It seems to work now. - Mua

1 Answers

5
votes

Use subprocess.Popen to run other executables in a subprocess.

Use multiprocessing.Process to run Python callables (e.g. functions) in a subprocess.

The two are not interchangeable.