I have been searching for a way to start and terminate a long-running "batch jobs" in python. Right now I'm using "os.system()" to launch a long-running batch job inside each child process. As you might have guessed, "os.system()" spawns a new process inside that child process (grandchild process?), so I cannot kill the batch job from the grand-parent process. To provide some visualization of what I have just described:
Main (grandparent) process, with PID = AAAA
|
|------> child process with PID = BBBB
|
|------> os.system("some long-running batch file)
[grandchild process, with PID = CCCC]
So, my problem is I cannot kill the grandchild process from the grandparent...
My question is, is there a way to start a long-running batch job inside a child process, and being able to kill that batch job by just terminating the child process? What are the alternatives to os.system() that I can use so that I can kill the batch-job from the main process ?
Thanks !!
subprocess module is the proper way to spawn and control processes in Python.
from the docs:
so... if you are on Python 2.4+,
subprocess
is the replacement foros.system
for stopping processes, check out the
terminate()
andcommunicate()
methods ofPopen
objects.If you are on a Posix-compatible system (e.g., Linux or OS X) and no Python code has to be run after the child process, use
os.execv
. In general, avoidos.system
and use thesubprocess
module instead.If you want control over start and stop of child processes you have to use threading. In that case, look no further than Python's
threading
module.