Skip to content

Instantly share code, notes, and snippets.

@amitt001
Created April 6, 2016 04:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amitt001/2cc5df6af5f28f9055040aab61a7c815 to your computer and use it in GitHub Desktop.
Save amitt001/2cc5df6af5f28f9055040aab61a7c815 to your computer and use it in GitHub Desktop.
Python subprocess module details
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