使用回调运行子流程

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从JSP或HTML向servlet发送多个参数   java方法来查看字符是否在字符数组中   使用带有java的MAC地址连接到设备   java如何将csv文件中的数据打印到secondactivity?   java如何从netbean 7.0.1连接到数据库   java考虑所有可能的类值,用于输出测试分割的预测值。   java我的actionListener调用有什么问题   swing在Java中实现粒子过滤器最有效的方法是什么?   java运行。getFontFamily()为返回null。使用apachepoi的docx文件   一个事务中的java领域循环与每个步骤循环中的一个事务   java日期格式与Spring Boot不兼容   java类冲突。处理   java GridBagLayout不工作   java将图像发送到另一个应用程序