Python thread Communication

Source: Internet
Author: User

Subprocess

Role

The module is used to produce a new process, connect to its input, output, error pipe, and get its return value

1. How to use the Subprocess module

The recommended way to start a child process is to use the following convenience features. For more advanced use cases, when these do not meet your needs, use the underlying Popen interface

    • subprocess.call(args, *, stdin=none, stdout=none, stderr=none, shell=false )

Function: Call system command as shown in args, wait for command to complete, return ReturnCode

Example:

>>> subprocess.call (["LS", "-l"]) 0>>> Subprocess.call ("Exit 1", shell=true) 1

Warning: Using Shell = True may be a security risk.

Note: Do not use stdout = pipe or stderr = pipe, as it may cause a deadlock in the output of the child process. When piping is required, use the Popen and communicate () method

    • subprocess.check_call(args, *, stdin=none, stdout=none, stderr=none, shell=false )

Run the command with parameters. Wait for the command to complete. If the return code is zero, then return, otherwise throw calledprocesserror. The Calledprocesserror object will have a return code in the ReturnCode property

Example:

>>> subprocess.check_call (["LS", "-l"]) 0>>> Subprocess.check_call ("Exit 1", Shell=true) Traceback ( Most recent call last):   subprocess. Calledprocesserror:command ' exit 1 ' returned Non-zero exit status 1
    • subprocess.check_output(args, *, stdin=none, stderr=none, shell=false, Universal_ Newlines=false)

Runs the system command and returns the output to a byte string, if the return value is not 0, throwsCalledProcessError异常。

Example:

>>> subprocess.check_output (["echo", "Hello world!"]) ' Hello world!\n ' >>> subprocess.check_output ("Exit 1", Shell=true) Traceback (most recent call last):   ... Subprocess. Calledprocesserror:command ' exit 1 ' returned Non-zero exit status 1

To capture the error message, use Stderr=subprocess. STDOUT:

>>> subprocess.check_output (... ")     LS Non_existent_file; Exit 0 ",...     Stderr=subprocess. STDOUT,...     shell=true) ' Ls:non_existent_file:No such file or directory\n '
    • Subprocess. PIPE

A special value, which can be used as the Stdin,stdout or stderr parameter of the Popen, and indicates that the pipe of the standard stream should be opened.

    • subprocess.STDOUT

Can be used as a special value for the stderr parameter of Popen, indicating that standard errors should enter the same handle as standard output.

    • To support a wide variety of use cases, popen constructors (and convenience functions) accept a large number of optional parameters. For most typical use cases, many of these parameters can be safely left with their default values. The most commonly needed parameters are:

      Args is required for all calls and should be a string or a series of program parameters. Supplying a parameter sequence is usually preferable because it allows the module to handle any required escape and reference parameters (for example, to allow spaces in the file name). If you pass a single string, the shell must be true (see below), otherwise the string must simply name the program to execute without specifying any parameters.

      Stdin,stdout and stderr specify standard input, standard output, and standard error file handles for the execution program, respectively. Valid values are pipe, existing file descriptor (positive integer), existing file object, and none. Pipe says a new pipe should be created for the child. With the default setting of None, no redirection occurs, and the child process's file handle is inherited from the parent process. In addition, stderr can be stdout, which indicates that stderr data from the child process should be captured in the same file handle as stdout.

      When stdout or stderr is a pipe, universal_newlines is true, all line endings are converted to ' \ n ', as described in the universal newline character ' U ' mode parameter of open ().

      If the shell is true, the specified command is executed through the shell. If you use Python primarily for enhanced control flow, it provides most system shells and still wants easy access to other shell features such as Shell pipeline, filename wildcard, environment variable extension and extension? to the user's home

This parameter is used for shell=true. At this point, we use an entire string instead of a table to run the child process. Python will run a shell first and then use this shell to interpret the entire string.

Example:

Import subprocessout = Subprocess.call ("Ls-l", shell=true) out = Subprocess.call ("CD:", Shell=true)
  • Popen Constructors
  • The underlying process in this module is created and managed by the Popen class. It provides a lot of flexibility so that developers can handle less common situations that are not covered by convenient features.

    Class subprocess. Popen (args,bufsize = 0,executable = None,stdin = None,stdout = None,stderr = NONE,PREEXEC_FN = None,close_fds = False,shel L = FALSE,CWD = None,env = None,universal_newlines = False, startupinfo = none,creationflags = 0)
    Executes the subroutine in the new process. On UNIX, classes use the OS.EXECVP ()-Class behavior to execute subroutines. On Windows, classes use the Windows CreateProcess () function. The parameters of the Popen are as follows.

    Args should be a sequence of program arguments or a single string. By default, if Args is a sequence, the program to execute is the first item in args. If Args is a string, the explanation is platform-dependent, as described below. See Shell and executable parameters for additional differences with the default behavior. Unless otherwise noted, it is recommended that you pass args as a sequence.

  • Example:
  • >>> import Shlex, subprocess>>> command_line = Raw_input ()/bin/vikings-input eggs.txt-output "spam sp Am.txt "-cmd" Echo ' $MONEY ' ">>> args = Shlex.split (command_line) >>> print args['/bin/vikings ', '- Input ', ' eggs.txt ', '-output ', ' spam spam.txt ', '-cmd ', ' echo ' $MONEY ' "]>>> p = subprocess. Popen (args) # success!
  • The shell parameter (false by default) specifies whether to use the shell as a program execution. If the shell is true, it is recommended that args be passed as a string instead of a sequence.

    On Unix, Shell = True,shell defaults to/bin/sh. If Args is a string, the string specifies the command to execute through the shell. This means that the format of the string must be exactly the same as when you typed it at the shell prompt. This includes, for example, a reference or backslash escape with spaces in the file name. If args is a sequence, the first item specifies the command string, and any other item is treated as an additional parameter to the shell itself. In other words, Popen is equivalent to:

  • Popen(['/bin/sh '-C 'args[0args[1...] ])
  • On windows with Shell = True, the COMSPEC environment variable specifies the default shell. The command you want to execute is built into the shell (for example, dir or copy) only when you need to specify Shell = True on Windows. You do not need Shell = True to run a batch file or a console-based executable file.
  • The bufsize (if given) has the same meaning as the corresponding parameter of the built-in open () function: 0 for unbuffered, 1 for row buffering, and any other positive values to use a buffer of size (approx.). Negative bufsize means using system defaults, which usually means full buffering. The default value for BufSize is 0 (unbuffered).
  • If you experience performance issues, try to enable buffering by setting BufSize to 1 or a positive value that is large enough (for example, 4096).
  • If PREEXEC_FN is set to a callable object, the object will be called in the child process before the child process executes. (Unix only)

    If Close_fds is true, all file descriptors except 0,1 and 2 will be closed before the child process executes. (Unix only). Or, on Windows, if Close_fds is true, the child process will not inherit any handles. Note that on Windows, you cannot set Close_fds to True, or you can redirect standard handles by setting stdin,stdout or stderr.

    If CWD is not none, the current directory of the child process will be changed to CWD before execution. Note that this directory is not considered when searching for executables, so you cannot specify the path of the program relative to CWD.

    If Env is not none, it must be a mapping of the environment variables that define the new process; These are the environments that are used instead of inheriting the current process, which is the default behavior.

  • Note If specified, env must provide any variables required by the program execution. On windows, the specified env must contain a valid systemroot in order to run the Side-by-side assembly.
    If Universal_newlines is true, the file object stdout and stderr are opened as text files in Universal line-wrapping mode. Rows can be terminated by the ' \ n ', Unix line-end conventions, ' \ R ', old Macintosh conventions, or ' \ r \ n ' (Windows conventions). All of these external representations are considered ' \ n ' by the Python program.

  • Exception
    • If the exception that is thrown in the child process before the new program starts executing, it is regenerated in the parent process. Additionally, the exception object will have an additional property called Child_traceback, which is a string that contains retrospective information from the child's perspective.

      The most common exception is oserror. For example, this happens when you try to execute a file that does not exist. The application should prepare the OSError exception.

      If you call Popen with an invalid parameter, ValueError is raised.

      If the called process returns a non-0 return code, Check_call () and Check_output () will throw calledprocesserror.

  • Security
    • Unlike other popen functions, this implementation does not implicitly invoke the system shell. This means that all characters, including Shell metacharacters, can be passed safely to the child process. Obviously, if the shell is explicitly called, then the application has the responsibility to ensure that all spaces and metacharacters are properly referenced.

Popen Object

    • Import Subprocesschild = subprocess. Popen (["Ping", "-C", "5", "www.google.com"]) child.wait () print ("Parent process")

      In addition, you can also perform other operations on the child process in the parent process, such as in our example:

      Child.poll () # Check child process status

      Child.kill () # terminating child process

      Child.send_signal () # Send a signal to a child process

      Child.terminate () # terminating child process

Popen.poll()

Popen.wait()

    • Popen.communicate (input = None)

Interacting with the process: sending data to stdin. Read data from stdout and stderr until the end of the file is reached. Wait for the process to terminate. The optional input parameter should be a string to send to the child process, or none if no data should be sent to the child process.

Communicate () returns a tuple (Stdoutdata,stderrdata).

Note that if you want to send data to the stdin of the process, you need to use stdin = pipe to create the Popen object. Similarly, to get any value except none in the result tuple, you need to give STDOUT = pipe and/or stderr = pipe.

Popen.stdin
If the stdin parameter is pipe, this property is a file object that provides input to the child process. Otherwise, it is none.

Popen.stdout
If the stdout parameter is pipe, this property is the file object that provides the output of the child process. Otherwise, it is none.

Popen.stderr
If the stderr parameter is pipe, this property is a file object that provides the error output of the child process. Otherwise, it is none.

Popen.pid
The process ID of the child process.

Note that if you set the shell parameter to True, this is the process ID of the generated shell.

Popen.returncode
The child return code, set by poll () and Wait () (indirectly through Communic ()). No value indicates that the process has not been terminated.

A negative-n indicates that the child node is terminated by the signal N (Unix only).

Python thread Communication

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.