python自动复制输出_python - 使用Python子过程复制过程输出 - SO中文参考 - www.soinside.com...

我正在尝试从Python自动化脚本中运行“ docker-compose pull”,并逐步显示与Docker命令直接从外壳运行时将输出的输出相同的输出。此命令为系统中找到的每个Docker映像打印一行,并在该Docker映像的最新版本下载完成后,在每行末尾递增“ done”。我首先尝试通过subprocess.poll()和(阻塞)readline()调用获取命令输出:import shlex

import subprocess

def run(command, shell=False):

p = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=shell)

while True:

# print one output line

output_line = p.stdout.readline().decode('utf8')

error_output_line = p.stderr.readline().decode('utf8')

if output_line:

print(output_line.strip())

if error_output_line:

print(error_output_line.strip())

# check if process finished

return_code = p.poll()

if return_code is not None and output_line == '' and error_output_line == '':

break

if return_code > 0:

print("%s failed, error code %d" % (command, return_code))

run("docker-compose pull")

代码被卡在第一个(阻塞的)readline()调用中。然后我尝试做同样的事情而不阻塞:import select

import shlex

import subprocess

import sys

import time

def run(command, shell=False):

p = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=shell)

io_poller = select.poll()

io_poller.register(p.stdout, select.POLLIN)

io_poller.register(p.stderr, select.POLLIN)

while True:

# poll IO for output

io_events_list = []

while not io_events_list:

time.sleep(1)

io_events_list = io_poller.poll(0)

# print new output

for event in io_events_list:

if event[0] == sys.stdout.fileno():

output_str = p.stdout.read().decode('utf8')

print(output_str, end="")

if event[0] == sys.stderr.fileno():

error_output_str = p.stderr.read().decode('utf8')

print(error_output_str, end="")

# check if process finished

# when subprocess finishes, iopoller.poll(0) returns a list with 2 select.POLLHUP events

# (one for stdout, one for stderr) and does not enter in the inner loop

return_code = p.poll()

if return_code is not None:

break

if return_code > 0:

print("%s failed, error code %d" % (command, return_code))

run("docker-compose pull")

io_poller.poll()从不返回任何IO事件。

这两种方法都可以与带有更简单输出(例如“ ls”)的命令配合使用。我做错了吗,或者问题可能与该Docker命令如何逐步打印到屏幕有关?通过Python脚本运行命令时,是否存在一种安全的方法来在命令行中逐步显示命令的确切输出?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值