1

I am a newbie in Python. I recently tried to use Python script to call a 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. It should release the CPU to other new jobs. So I think I may need the multiple process control mechanism.

Since multiprocessing can only call python callable function. It can not directly call the console exe. I wrapped the subprocess.Popen(cmd) in a python function. However, after I did so, I found that before multiprocessing.Process.start() is used. The exe has already started. And problem is before it finish what it is doing (need long time), it does not return me the control. The program is freezed to wait. That is not what I want.

I am posting the codes as below:

import sys 

import os 

import multiprocessing 

import subprocess  

def subprocessExe(cmd):

        return subprocess.call(cmd, shell=False, stdout=subprocess.PIPE, \
                               stderr=subprocess.PIPE,creationflags=0x08000000)

if __name__ == '__main__': 

        p = multiprocessing.Process(target=self.subprocessExe(exeFileName))

        p.start()

        print p, p.is_alive()

Thank you for your time!

khachik
  • 28,112
  • 9
  • 59
  • 94
Mua
  • 31
  • 1
  • 6

2 Answers2

2

You're calling subprocessExec when you create the multiprocessing.Process object. You should instead do this:

p = multiprocessing.Process(target=subprocessExec, args=(exeFileName,))

And then it should work.

  • I tried this. It does not work. Got an error in Python's own forking.py module. – Mua May 26 '11 at 14:09
  • What I used is exactly what you wrote there. But my filename has a space there. because it has arguments followed. So my "cmd" is like something "*.exe xyz". It has no problem with subprocess.Popen. But if I put the whole string "cmd" in arg of multiprocessing.processing. It report error of "picklingerror" – Mua May 26 '11 at 14:12
  • Tried again without space in cmd string. Only a exe file. It still report that error. I am using python 2.6. – Mua May 26 '11 at 14:19
1

There are number of things that are wrong in your test case. The following work for me:

import multiprocessing, subprocess

def subprocessExe(cmd):
   subprocess.call([cmd], shell=False)

p = multiprocessing.Process(target=subprocessExe, args=('/full/path/to/script.sh',))
p.start()

OR

subprocess.call([cmd], shell=True)
p = multiprocessing.Process(target=subprocessExe, args=('script.sh',))

OR

subprocess.Popen([cmd], shell=True, stdout=subprocess.PIPE, \
                               stderr=subprocess.PIPE)
Ilker S
  • 93
  • 1
  • 8