Reputation: 135
I have a probably naive question regarding python system call behavior. I am wondering if os.system()
will return right away after the cmd is called or it will wait any the cmd finish to return.
Similarly question for subprocess module. I think subprocess will return right way after it was called as it spawns a child process. But I am not sure about os.system()
. There is no doc regarding this.
Thanks.
Upvotes: 0
Views: 311
Reputation: 74645
The
system()
function shall not return until the child process has terminated.
From http://pubs.opengroup.org/onlinepubs/009695399/functions/system.html which the Python library refers to when it states:
This is implemented by calling the Standard C function
system()
, and has the same limitations.
From https://docs.python.org/2/library/os.html#os.system
Upvotes: 1
Reputation: 280500
The os.system
documentation says the following:
On Unix, the return value is the exit status of the process encoded in the format specified for wait(). Note that POSIX does not specify the meaning of the return value of the C system() function, so the return value of the Python function is system-dependent.
On Windows, the return value is that returned by the system shell after running command, given by the Windows environment variable COMSPEC: on command.com systems (Windows 95, 98 and ME) this is always 0; on cmd.exe systems (Windows NT, 2000 and XP) this is the exit status of the command run; on systems using a non-native shell, consult your shell documentation.
For os.system
to return these values, it must wait for the program to complete and provide an exit code. Also, it says
This is implemented by calling the Standard C function system(), and has the same limitations.
The documentation for the C system
function says
system() executes a command specified in command by calling /bin/sh -c command, and returns after the command has been completed.
So it has to wait for the command to complete.
As for subprocess
, the call
, check_call
, and check_output
convenience function all wait for the command to complete. call
and check_call
say this explicitly, while check_output
returns the command's output as a byte string, which requires it to wait for the command to finish.
subprocess.Popen
does not wait for the command to complete. It has several methods to check whether the command has completed or wait for the command to complete, such as Popen.wait
.
Upvotes: 1