Use "os" Module to Create Child Processes

This section provides tutorial examples on how to use os.system(), os.execl(), os.fork(), os.popen() methods to create child processes in different ways.

"os" module also offers a number of methods to create and manage execution processes.

1. os.system(cmd) - Method executes the given command in a sub-shell, waits until the command ends, and returns the exit code of the command.

>>> os.system('ping -c 1 localhost')
PING localhost (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.050 ms

--- localhost ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.050/0.050/0.050/0.000 ms
0

2. os.execl(path, arg0, arg1, ...) and os.execv(path, args) - Methods load the executable program from the give path and runs it with given arguments.

>>> os.execl('/sbin/ping', 'fake.py', '-c', '1', 'localhost')
PING localhost (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.059 ms

--- localhost ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.059/0.059/0.059/0.000 ms

herong$

Note that arg0 or args[0] is a placeholder for the script name. So arguments passed to the executable program starts from arg1 or args[1].

Also note that the loaded program will replace and take over the current process. So your script will lost control after calling os.exec*() methods.

3. os.fork() - Method creates a child process and runs a copy of the running script in the child process. The method in the current process returns the process id (a positive number) of the child process. The copied method in the child process returns 0.

If os.fork() fails to run, it raises the OSError and returns a negative number.

herong$ more fork_test.py
import os
id = os.fork()
if id == 0:
    # code for the child process
    pid = os.getpid()
    print(f"Child process with id {pid}")
else:
    # code for the current process
    if id > 0:
        pid = os.getpid()
        print(f"Current process with my id {pid} and my child id {id}")
    else:
        print(f"Failed to create child process")

herong$ python3 fork_test.py
Current process with id of 32340 and holding child process id of 32341
Child process with id of 32341

4. os.popen(cmd, 'r/w') - Method execute the given command in a child process, and returns immediately a file descriptor of the stdout/stdin stream. You can read/write data from/to the file descriptor.

When the file descriptor is closed, the child process will be terminated if its not ended yet.

fd = os.popen("ping localhost", 'r')
data = fd.read(80)
print(data)

fd.close()

Table of Contents

 About This Book

 Running Python Code Online

 Python on macOS Computers

 Python on Linux Computers

 Built-in Data Types

 Variables, Operations and Expressions

 Statements - Execution Units

 Function Statement and Function Call

 Iterators, Generators and List Comprehensions

 Classes and Instances

 Modules and Module Files

 Packages and Package Directories

"sys" and "os" Modules

 What Is "sys" Module

 What Is "os" Module

Use "os" Module to Create Child Processes

 Use "os" Module to Manage Files

 "pathlib" - Object-Oriented Filesystem Paths

 "pip" - Package Installer for Python

 SciPy.org - Python Libraries for Science

 pandas - Data Analysis and Manipulation

 Anaconda - Python Environment Manager

 Jupyter Notebook and JupyterLab

 References

 Full Version in PDF/EPUB