Start working with processes in Python
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.
Getting ready
In this first Python application, you'll simply get the Python language installed.
Note
Refer to https://www.python.org/ to get the latest version of Python.
How to do it…
To execute this first example, we need to type the following two programs:
called_Process.py
calling_Process.py
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!!")
To run the example, open the calling_Process.py
program with the Python IDE and then press the F5 button on the keyboard.
You will see the following output in the Python shell:
At same time, the OS prompt displays the following:
We have two processes running to close the OS prompt; simply press the Enter button on the keyboard to do so.
How it works…
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 input()
in 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.