使用回调运行子流程

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()

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java使用(BluetoothGattCharacteristic)特性中的数据创建、写入和保存csv文件。getValue()   java如何使用groupBy创建一个值为BigDecimal字段平均值的映射?   日期时间Java将iso_即时格式的字符串转换为日期   java如何检索和显示Android firebase的配置文件?   scala AWSJAVASDK:解压缩大小必须小于262144000字节   要应用于列表的java JSTL if条件   java在3个点之间画一个正方形   Kotlin java抽象类IllegaAccessError   java原语双值相等取决于大小?   java有没有一种方法可以对数据集使用compareTo()方法,而不必遍历数据集的每个元素?   java为什么我发送的每个UDP消息都会改变端口源?   重新选择文件时swing Java JTree冻结   java不知道我的游戏是怎么回事   Motif L&F中自定义组合框渲染器中的Java Swing消失文本   java Trade Me API访问前的OAuth