English

How To: Run multiple Python scripts from a single primary 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 secondary 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 primary script that spawns two secondary scripts:

import os
from subprocess import *

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

#run secondary script 2
p = Popen([r'C:\secondaryScript2.py', "ArcEditor"], shell=True, stdin=PIPE, stdout=PIPE)
output = p.communicate()
print output[0]
 Secondary script 1
import sys, arcgisscripting
gp = arcgisscripting.create(9.3)
print gp.setproduct(sys.argv[1])
Secondary script 2
import sys, arcgisscripting
gp = arcgisscripting.create(9.3)
print gp.setproduct(sys.argv[1])

Related Information

Last Published: 9/14/2020

Article ID: 000010647