On common operating systems, each program runs in its own process. Usually, we start a program by double-clicking on the icon's program or selecting it from a menu. In this recipe, we simply demonstrate how to start a single new program from inside a Python program. A process has its own space address, data stack, and other auxiliary data to keep track of the execution; the OS manages the execution of all processes, managing the access to the computational resources of the system via a scheduling procedure.
In this first Python application, you'll simply get the Python language installed.
Refer to https://www.python.org/ to get the latest version of Python.
To execute this first example, we need to type the following two programs:
You can use the Python IDE (3.3.0) to edit these files:
The code for the
called_Process.py file is as shown:
print ("Hello Python Parallel Cookbook!!") closeInput = raw_input("Press ENTER to exit") print "Closing calledProcess"
The code for the
calling_Process.py file is as shown:
##The following modules must be imported import os import sys ##this is the code to execute program = "python" print("Process calling") arguments = ["called_Process.py"] ##we call the called_Process.py script os.execvp(program, (program,) + tuple(arguments)) print("Good Bye!!")
You will see the following output in the Python shell:
At same time, the OS prompt displays the following:
In the preceding example, the
execvp function starts a new process, replacing the current one. Note that the "Good Bye" message is never printed. Instead, it searches for the program
called_Process.py along the standard path, passes the contents of the second argument tuple as individual arguments to that program, and runs it with the current set of environment variables. The instruction
called_Process.py is only used to manage the closure of OS prompt. In the recipe dedicated to process-based parallelism, we will finally see how to manage a parallel execution of more processes via the multiprocessing Python module.