使用回调运行子流程
subprocessor的Python项目详细描述
在子进程中运行命令,并从stdout和stderr生成文本行
示例
importsubprocessorassubCMD='my-unix-command "My Cool File.txt" No-file.txt'forok,lineinsub.Sub(CMD)assp:ifok:print(' ',line)else:print('!',line)ifsp.returncode:print('Error code',sp.returncode)# Return two lists of text lines and a returncodeout_lines,err_lines,returncode=sub.run(CMD)# Call callback functions with lines of text read from stdout and stderrreturncode=sub.call(CMD,save_results,print_errors)# Log stdout and stderr, with prefixesreturncode=sub.log(CMD)
美国石油学会
class subprocessor.Sub:
上的方法Sub.__init__(self, cmd, **kwds)
Iterate over lines of text from a subprocess.
If ^{tt3}$ is true, ^{tt4}$ expects a string, and so if ^{tt5}$ is not a string, it is joined using ^{tt6}$.
If ^{tt3}$ is false, ^{tt4}$ expects a list of strings, and so if ^{tt5}$ is a string, it is split using ^{tt6}$.
- ARGUMENTS
- cmd:
- The command to run in a subprocess: a string or a list or tuple of strings
- kwargs:
- Keyword arguments passed to subprocess.Popen()
Sub.__iter__(self)
Yields a sequence of ^{tt12}$ pairs from ^{tt13}$ and ^{tt14}$ of a subprocess, where ^{tt15}$ is ^{tt16}$ if ^{tt17}$ came from ^{tt13}$ and ^{tt19}$ if it came from ^{tt14}$.
After iteration is done, the ^{tt21}$ property contains the error code from the subprocess, an integer where 0 means no error.
Sub.call(self, out=None, err=None)
Run the subprocess and call function ^{tt23}$ with lines from ^{tt13}$ and function ^{tt25}$ with lines from ^{tt14}$.
Blocks until the subprocess is complete: the callbacks are on the current thread.
- ARGUMENTS
- out:
- if not None, ^{tt23}$ is called for each line from the subprocess’s stdout
- err:
- if not None, ^{tt25}$ is called for each line from the subprocess’s stderr,
Sub.run(self)
Reads lines from ^{tt13}$ and ^{tt14}$ into two lists ^{tt23}$ and ^{tt25}$, then returns a tuple ^{tt34}$
Sub.log(self, out=' ', err='! ', print=<built-in function print>)
Read lines from ^{tt36}$ and ^{tt14}$ and prints them with prefixes
Returns the shell integer error code from the subprocess, where 0 means no error.
- ARGUMENTS
- out:
- Prefix for printing lines from stdout
- err:
- Prefix for printing lines from stderr
功能
subprocessor.call(cmd, out=None, err=None, **kwds)
Run the subprocess and call function ^{tt23}$ with lines from ^{tt13}$ and function ^{tt25}$ with lines from ^{tt14}$.
Blocks until the subprocess is complete: the callbacks are on the current thread.
- ARGUMENTS
- cmd:
- The command to run in a subprocess: a string or a list or tuple of strings
- out:
- if not None, ^{tt23}$ is called for each line from the subprocess’s stdout
- err:
- if not None, ^{tt25}$ is called for each line from the subprocess’s stderr,
- kwargs:
- Keyword arguments passed to subprocess.Popen()
subprocessor.run(cmd, **kwds)
Reads lines from ^{tt13}$ and ^{tt14}$ into two lists ^{tt23}$ and ^{tt25}$, then returns a tuple ^{tt34}$
- ARGUMENTS
- cmd:
- The command to run in a subprocess: a string or a list or tuple of strings
- kwargs:
- Keyword arguments passed to subprocess.Popen()
subprocessor.log(cmd, out=' ', err='! ', print=<built-in function print>, **kwds)
Read lines from ^{tt36}$ and ^{tt14}$ and prints them with prefixes
Returns the shell integer error code from the subprocess, where 0 means no error.
- ARGUMENTS
- cmd:
- The command to run in a subprocess: a string or a list or tuple of strings
- out:
- Prefix for printing lines from stdout
- err:
- Prefix for printing lines from stderr
- kwargs:
- Keyword arguments passed to subprocess.Popen()
- 项目
标签: