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):
        procs.append(spawn())

    rawData = {}

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

        if proc.stderr:
            error = proc.stderr.read()

            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[proc.pid] = data

    print rawData


if __name__ == "__main__":
    main()

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