Spawning Python Processes

Here is a quick and simple way of spawning process in Python using the subprocess module

import errno
from os import waitpid, P_WAIT
from subprocess import Popen, PIPE

def spawn():
    cmd = "echo 'a,b,c,d,e\nf,g,h,i,j'"
    p = Popen(cmd, shell=True, stdout=PIPE, env={"PATH": "/bin"})
    return p

def main():
    procs = []

    for x in (0,1,2,3,4,5,6):

    rawData = {}

    for proc in procs:
            waitpid(, P_WAIT)
        except Exception, e:
            if e.errno != errno.ECHILD:
                raise e

        if proc.stderr:
            error =

            if error:
                msg = "Error in spawened process, %s" % error
                raise OSError(msg)

        data = [[out.strip() for out in line.split(',')]
            for line in proc.stdout.readlines()]
        rawData[] = data

    print rawData

if __name__ == "__main__":

SpawnPythonProcesses (last edited 2009-05-26 01:41:26 by CarlNobile)