In Python, What Are Some Disadvantages to Using os.execlp to Fork a Process?

Question
In Python you are familiar with importing the os module and using different exec variations. What are some reasons that you would not use os.execlp?

Answer
1. If you use os.execlp to call another program, that program is more likely to return "Killed". The resources of the child process are, by default, more limited in part because the fork operation is expensive from a system's resources perspective. Here is an example of two programs (where bar.py calls foo.py). If you run the top program by itself, it will work on a low-powered system:

# Program #1 (foo.py)
import os, datetime, time
t1 = datetime.datetime.now()

def wastetime(n):
  x = 0
  for i in range(n):
    x = x + i

wastetime(30000000)

t2 = datetime.datetime.now()
t3 = t2 - t1
print("Time format is in hours:minutes:seconds:seconds_decimals")
print(t3)
# Program #2 (bar.py)
import os, cProfile

def callprog():
  os.execlp('python', 'python', 'foo.py')

cProfile.run('callprog()')

But if you run the bottom program, when it is in the same directory as the top program, it will not work. The bottom program will returned "Killed" -- even though all it is doing is running the top program. (You may want to read How Do You Fix a Python Program that Returns “Killed”?)

2. You cannot see the output of cProfile with the called program. cProfile will give you statistics on function calls. If you could run the called program directly, you can benefit from seeing the cProfile output. But if the program is invoked from an os.execlp() call, the called program's cProfile output will not display.

Here is a program that will display cProfile output to help illustrate why you would not use os.execlp():

# You can name it test2.py and run it with "python test2.py"
import os, datetime, time, cProfile
t1 = datetime.datetime.now()

def wastetime(n):
  x = 0
  for i in range(n):
    x = x + i

cProfile.run('wastetime(20000000)')

t2 = datetime.datetime.now()
t3 = t2 - t1
print("Time format is in hours:minutes:seconds:seconds_decimals")
print(t3)

# If you have a second program to call that program above, no cProfile output will be visible.

Leave a comment

Your email address will not be published. Required fields are marked *