English

How To: Run multiple Python scripts from a single parent script.

Summary

Instructions provided give scripting examples that demonstrate how to use Python's subprocess module to spawn a new process while providing inputs to and retrieving outputs from the child script.

The subprocess module is new in version 2.4 of Python and is provided as a standard module. This module intends to replace several other, older modules and functions, such as:

os.system
os.spawn*
os.popen*
popen2.*
commands.*

See the Python documentation for additional information about this module.

Procedure

The following is a parent script that spawns two child scripts:

Code:
import os
from subprocess import *

#run child script 1
p = Popen([r'C:\childScript1.py', "ArcView"], shell=True, stdin=PIPE, stdout=PIPE)
output = p.communicate()
print output[0]

#run child script 2
p = Popen([r'C:\childScript2.py', "ArcEditor"], shell=True, stdin=PIPE, stdout=PIPE)
output = p.communicate()
print output[0]

  1. Child script 1
    Code:
    import sys, arcgisscripting
    gp = arcgisscripting.create(9.3)
    print gp.setproduct(sys.argv[1])

  2. Child script 2
    Code:
    import sys, arcgisscripting
    gp = arcgisscripting.create(9.3)
    print gp.setproduct(sys.argv[1])

Related Information