Reputation: 1434
Assume using Linux:
In Perl, the exec
function executes an external program and immediately exits itself, leaving the external program in same shell session.
A very close answer using Python is https://stackoverflow.com/a/13256908
However, the Python solution using start_new_session=True
starts an external program using setsid method, that means that solution is suitable for making a daemon, not an interactive program.
Here is an simple example of using perl:
perl -e '$para=qq(-X --cmd ":vsp");exec "vim $para"'
After vim is started, the original Perl program has exited and the vim is still in the same shell session(vim is not sent to new session group).
How to get the same solution with Python.
Upvotes: 3
Views: 385
Reputation: 1122282
Perl is just wrapping the exec*
system call functions here. Python has the same wrappers, in the os
module, see the os.exec*
documentation:
These functions all execute a new program, replacing the current process; they do not return. On Unix, the new executable is loaded into the current process, and will have the same process id as the caller.
To do the same in Python:
python -c 'import os; para="-X --cmd \":vsp\"".split(); os.execlp("vim", *para)'
os.execlp
accepts an argument list and looks up the binary in $PATH
from the first argument.
The subprocess
module is only ever suitable for running processes next to the Python process, not to replace the Python process. On POSIX systems, the subprocess
module uses the low-level exec*
functions to implement it's functionality, where a fork of the Python process is then replaced with the command you wanted to run with subprocess
.
Upvotes: 8