Python subprocess Popen

Source: Internet
Author: User
Tags python subprocess

Purpose: Sequential execution process inside bash like a.sh && b.sh && c.sh

First of all, the Popen function.

class subprocess. Popen (args, bufsize=0, executable=none, stdin=none, stdout=none , stderr=none, preexec_fn=none, close_fds=false,

        shell=false, cwd=none, env=none, universal_newlines=false, startupinfo= None,creationflags=0)

    1. args child process cmd
    2. bufsize Default is 20K
    3. stdin Standard input
    4. STDOUT standard output
    5. STDERR standard Error
    6. Other parameters skipped .....

    • Popen. poll () Check if child process terminates, return ReturnCode
    • Popen. wait () waiting for the child process to terminate, return ReturnCode, it should be noted that if Popen executes the command, set stdout=pipe or stderr=pipe, the child process will produce enough output, This will cause the OS's pipeline cache to be blocked by the time it takes to get the data, which requires the communicate () function.
    • Popen. Communicate (input=none) This is a method of interacting with the process that sends the data to the child process's standard input until the process is finished. If you do not require the transfer of data to the child process settings Input=none

communicate () returns a tuple (stdoutdata, stderrdata).

If you want to send data to the child process standard input, you need to set the stdin=pipewhen creating the Popen. If you want to get information from a tuple, you have to set stdout=pipe or/and stderr=pipe.

This method of data is read after the cache in memory, if the amount of data is very large do not recommend this method

Example:

Import subprocess
Import OS
Import Sys

def run_application (command):
Try
Process = subprocess. Popen (command, stdout=subprocess. PIPE, Stderr=subprocess. PIPE)
communicate = Process.communicate (Input=none)
Return communicate
Except Exception:
Print "Error"
Sys.exit (-1)

if __name__ = = "__main__":
cmd = "ls"

result = Run_application (cmd)
If RESULT[0]:
Run_application (CMD)
print "Done"

Python subprocess Popen

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.