Python OS Processes
The Python programming language contains extensive libraries of standardized pre-written code that make everyday programming tasks easy and efficient. One of these libraries, the "os" library, contains functions used to interact with the programmer's operating system. Because of this, Python programmers can gain important data about the state of the system. The programmer can also create "processes," or separate instances of a program that split (or "fork") from and run concurrently with the main program. These process run in memory with the main program, executing the same code. This allows a multiple computation to happen concurrently in the span of the same program.
-
The "os" Module
-
The "os" module included with Python gives programmers access to many aspects of their systems through an intuitive Python interface. The functionality of the os module ranges from simple access to variables in the system (such as the system name) to monitoring the environment of the user. The following example shows how a programmer imports the os module and uses some of its built-in features:
>>>import os
>>>os.name
'nt'
>>>os.getcwd() //returns current working directory
'C:\\Python27'
The os Module and Processes
-
The os module can also help the programmer retrieve information about the currently running program. A Python program can use the functions in the os module to gather data regarding the program's process id (PID) numbers, or get information about the user or group executing the program during code execution. Note: Most methods that retrieve data about user and group ids only work on Unix operating systems. Certain methods only work on systems they were designed for. For example, the following code gets the user's process id and the user's user and group ids.
>>>os.getpid() //Windows and Unix
1500
>>os.getuid() //Unix only
85
>>os.getgid() //Unix only
34
-
Creating Processes
-
Python programmers can use the "fork()" method to spawn another instance of the program. When this happens, another copy of the program loads, and both the parent and the child programs continue to execute the same code. In the following example, the fork() function creates a new process, and both processes will run an "if" statement. The parent process (the process executing the "fork()" command) will print the parent id, and the child process will print its own id:
cpid = os.fork() //fork() returns child PID
if cpid == 0: //the child process will not have stored its id into the variable
print "Child: %s" % os.getpid()
else:
print "Parent: %s" % os.getpid()
Controlling Processes
-
As Python can create processes, the os module also supplies methods to manage them. The "wait()" method and its variations control how a process manages its child processes. The main process can just wait until any process completes, as in wait(). Or it can wait for a specific process using the waitpid() method. Finally, the parent process can use the "wait3()" and "wait4()" methods to gather information about the processes when they stop execution.
Using Processes
-
Using multiprocess programs allows the programmer to develop applications to do multiple things at once. A typical application using multiprocessing is written for use on processors with multiple cores. The programmer can use processes spawned from a program to perform calculations across multiple cores, maximizing calculation speed and efficiency. Or, the programmer can use a process to execute a listening algorithm, which waits for a signal from an internet connection before activating and sending information to the main program.
-