Executing External Commands Asynchronously from Python
Executing external commands asynchronously is a common requirement in scripting scenarios. In Python, this can be achieved through various approaches.
One option is using os.system, which allows running commands non-blocking by appending an ampersand (&) at the end. However, this method is considered deprecated and not recommended due to potential issues with shell interaction.
A more optimal approach is using subprocess.Popen, which provides a more comprehensive and robust API for managing external processes. With Popen, commands can be launched asynchronously, allowing the Python script to continue execution while the external command runs in the background.
from subprocess import Popen
p = Popen(['watch', 'ls']) # something long running
# ... do other stuff while subprocess is running
p.terminate()
In this example, the Popen instance is created, passing the command and its arguments. The Python script can then proceed with other tasks while the external command runs asynchronously. Later, the Popen instance can be queried for status (e.g., using poll()), communicated with (e.g., via communicate()), or terminated.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3