Subprocess.Popen () using the solutions to problems

from subprocess import Popen,PIPE

1. cursor is blinking wait state, the test can not be output in real time cmd interface.

[Reason]: Use communicate () function, need to wait for the script finished execution before returning.

def communicate(self, input=None):

    """Interact with process: Send data to stdin.  Read data from stdout and stderr, until end-of-file is reached.  Wait for process to terminate.  
The optional input argument should be a string to be sent to the child process, or None, if no data should be sent to the child.

communicate() returns a tuple (stdout, stderr).
"""

[Solution]: Function replaced by subprocess.poll (), the program finishes running Back 1. otherwise None / 0.

poll DEF (Self): 
"". "Child Process has terminated the Check IF and return the Set ReturnCode attribute." ""
return self._internal_poll ()

2. Output results do not wrap, a large lump ...
[Cause]: Unused
[Solution]: a while loop binding readline + poll () function, a line out of the line of printing stdout.readline .... ()



the Fail Code:
1         p1 = Popen(['bash.exe', 'run_all_tests.sh', 'win32'], stdout=PIPE, shell=True)
2         print repr(p1.communicate()[0])  #communicate 函数

 PASS Code:


#
1 p1 = Popen(['bash.exe', 'run_all_tests.sh', 'win32'], stdout=subprocess.PIPE, shell=True) 2 while p1.poll() is None: 3 data = p1.stdout.readline() 4 print data 5 #print repr(p1.communicate()[0])

'' '# Plurality of layers following cycle, to print a character xxx 
 . 3 p1.poll the while () None IS: 
 . 4 for Line in p1.stdout.readline (): 
 . 5 Print Line 
 . 6' ''
 

 

3. The transmission system of variable env path in two ways.

3.1 imitate the command line, windows with a set to set

3.2 [Recommended] NA set command line parameters transmitted directly to the my_env subpross.Popen () to initialize the class. Such problems can be effectively avoided symbol string escaping

View Popen class Help documentation is as follows

 

 1 class Popen(object):
 2     """ Execute a child program in a new process.
 3 
 4     For a complete description of the arguments see the Python documentation.
 5 
 6     Arguments:
 7       args: A string, or a sequence of program arguments.
 8 
 9       bufsize: supplied as the buffering argument to the open() function when
10           creating the stdin/stdout/stderr pipe file objects
11 
12       executable: A replacement program to execute.
13 
14       stdin, stdout and stderr: These specify the executed programs' standard
15           input, standard output and standard error file handles, respectively.
16 
17       preexec_fn: (POSIX only) An object to be called in the child process
18           just before the child is executed.
19 
20       close_fds: Controls closing or inheriting of file descriptors.
21 
22       shell: If true, the command will be executed through the shell.
23 
24       cwd: Sets the current directory before the child is executed.
25 
26       env: Defines the environment variables for the new process.
27 
28       universal_newlines: If true, use universal line endings for file
29           objects stdin, stdout and stderr.
30 
31       startupinfo and creationflags (Windows only)
32 
33     Attributes:
34         stdin, stdout, stderr, pid, returncode
35     """
36     _child_created = False  # Set here since __del__ checks it
37 
38     def __init__(self, args, bufsize=0, executable=None,
39                  stdin=None, stdout=None, stderr=None,
40                  preexec_fn=None, close_fds=False, shell=False,
41                  cwd=None, env=None, universal_newlines=False,
42                  startupinfo=None, creationflags=0):
43         """Create new Popen instance."""
View Code

 So the code parameters can be added:

 

1 p1 = Popen(['bash.exe', 'run_all_tests.sh', 'win32'], stdout=PIPE, shell=True, env=my_env)

 

Guess you like

Origin www.cnblogs.com/ASAP/p/10939548.html