Created
April 6, 2016 04:30
-
-
Save amitt001/2cc5df6af5f28f9055040aab61a7c815 to your computer and use it in GitHub Desktop.
Python subprocess module details
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Subprocess: | |
One class Popen and others are wrapper over it. | |
shell=True: This argument spawns a new intermediate process and run the command. If False, the command run in current process. | |
Popen: non blocking | |
subprcess call vs Popen: | |
call: is a wrapper over Popen and takes all the pamaters of popen. It is blocking and does not return until process has finished. | |
So, stdout=PIPE shall not be used with this becuase it keeps stdout in memeory till child process exit and child process will hang as soon as it fills the OS pipe buffer. | |
Popen: Non-blocking. Can be made blocking with wait(). | |
wait():BLOCKING Waits for the child process to terminate and return returncode | |
poll(): checks child process terminated? return returncode else None | |
communicate():BLOCKING Iteract with the process. send data to stdin. Read data from stderr and stdout till the end of the file. Wait for the process to terminate. Returns a tuple (stdout, stderr) | |
shell=True : Avoid shell=True by all means. Special chafracters must be escaped, allows user to run arbitrary programs on shell |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment