使用回调运行子进程

sproc的Python项目详细描述


在子进程中运行命令,并从stdout和stderr生成文本行

示例

importsprocCMD='my-unix-command "My Cool File.txt" No-file.txt'forok,lineinsproc.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=sproc.run(CMD)# Call callback functions with lines of text read from stdout and stderrreturncode=sproc.call(CMD,save_results,print_errors)# Log stdout and stderr, with prefixesreturncode=sproc.log(CMD)

美国石油学会

class sproc.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 to ^{tt23}$ and ‘err`` 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.call_async(self, out=None, err=None)

Run the subprocess, and asynchronously call function ^{tt23}$ with lines from ^{tt13}$, and function ^{tt25}$ with lines from ^{tt14}$.

Does not block - immediately returns.

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 ^{tt42}$

Sub.log(self, out='  ', err='! ', print=<built-in function print>)

Read lines from ^{tt44}$ 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

功能

sproc.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 to ^{tt23}$ and ‘err`` 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()

sproc.call_async(cmd, out=None, err=None, **kwds)

Run the subprocess, and asynchronously call function ^{tt23}$ with lines from ^{tt13}$, and function ^{tt25}$ with lines from ^{tt14}$.

Does not block - immediately returns.

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

sproc.run(cmd, **kwds)

Reads lines from ^{tt13}$ and ^{tt14}$ into two lists ^{tt23}$ and ^{tt25}$, then returns a tuple ^{tt42}$

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

sproc.log(cmd, out='  ', err='! ', print=<built-in function print>, **kwds)

Read lines from ^{tt44}$ 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的测试   java如何编写在请求体中接受XML的swagger API   java PrimeTable面临奇怪的错误   java如何检查数组中输入的用户是否为回文?   java如何删除JButton中文本周围的框?   java阻止直接访问JSF2中的xhtml文件   java如何获取定义方法的类的名称?   while loop如何让用户只需输入数字,然后在Java中重试?   从应用程序注册中列出azure存储帐户容器时,java受众验证失败   Java线程之间的多线程数据交换   java检查数组中是否存在重复的索引值?   java正则表达式从字符串中复制第二个URL   java如何从gradle项目依赖项中排除METAINF?   java如何将JLabel[]添加到JTable?   使用kotlin播放java音频(位于internet上的文件)