Sublaunching Other Programs
1. Introduction
There will probably come a time when you will want to automate some of the features in another program. A Python script can "launch" another program and command it to do something as a "subprocess". Process is just the name of anything (programs or services) that are running on a computer so a "subprocess" is a process that runs "under" another process. In our case, we'll be running a process under a Python script (which is running in a process). This is a very powerful capability but it can be a bit tricky to get used to.
There are a large number of libraries you can access through Python but there is an even larger number of "command-line" programs out there and there are times when you may want to call a command-line program from Python even if it has "Python bindings" available for it.
2. Getting Started
The code below will launch Internet Explorer and have it go to a specific URL. The "subprocess" package contains the functions to launch other processes. For this to work for you, you may need to change the location if Internet Explorer. Try the code below and then try your own URLs.
import subprocess subprocess.call("C:\\Program Files\\Internet Explorer\\iexplore.exe http:\\\\www.humboldt.edu")
3. Passing Arguments
Below is a modification to our MatPlotLib example that now takes arguments (parameters) from a command line. Paste this code into Wing and take a look at it. When you have a feel for how the code works, run it in Wing. Now try running it through a command line window giving it three parameters: mu (the mean or center of the distribution), sigma (the variance), and the number of values to generate.
###################################################################################### # Example of displaying a histogram as a sub-process # Inputs: mu, sigma, NumberOfValues # Adapted from the MatPlotLib tutorials # Author: Jim Graham # Date: 4/20/2014 ###################################################################################### import numpy as np import matplotlib.pyplot as plt import sys import tkMessageBox import Tkinter # keep the Tkinter windows from appearing TheRoot=Tkinter.Tk() TheRoot.withdraw() # setup default values mu=0 sigma=100 NumberOfValues=1000 # get the arguments from the command line TheArguments=sys.argv if (len(TheArguments)>1): # only set the arguments if there are more than one mu=float(TheArguments[1]) sigma=float(TheArguments[2]) NumberOfValues=int(TheArguments[3]) # this message box is just for debugging tkMessageBox.showinfo("HI","mu="+format(mu)+" sigma="+format(sigma)+" Number="+format(NumberOfValues)) # create the random values x = mu + sigma * np.random.randn(NumberOfValues) # create histogram of the data n, bins, patches = plt.hist(x, 50, normed=1, facecolor='g', alpha=0.75) # find the range of values for the axis xmin=mu-sigma*3 xmax=mu+sigma*3 # setup and display the histogram plt.xlabel('Value') plt.ylabel('Frequency') plt.title('Histogram') plt.text(60, .025, r'$\mu=100,\ \sigma=15$') plt.axis([xmin, xmax, 0, 0.03]) plt.grid(True) plt.show()
Now copy the code below into a new Wing Python file. This code will create a "subprocess" to run create the histogram. Single step through the code with Task Manager up and you'll see the new processes be created. Python actually creates "Applications" for each process.
import sys import subprocess # edit the path below to go to your script file saved from above ScriptPath="C:\\Temp\\MatPlotLib_Histogram.py"
# path to the version of python we want to launch
PythonPath="C:\\Python33\\python.exe"; # parameters for the random distribution mu=100 sigma=15 NumberOfValues=100 # subprocess call to put up the histogram subprocess.call("python "+ScriptPath+" "+format(mu)+" "+format(sigma)+" "+format(NumberOfValues))
Try running the script above from a tool in ArcGIS and you should see that running subprocesses makes it much harder to crash ArcGIS.