Python subprocess.communicate 无法从 运行c 容器捕获输出,如果不是来自交互式终端的 运行

Python subprocess.communicate fails to capture output from runc container if not run from interactive terminal

我有一个脚本,它 运行 是一个子进程并捕获输出,但只有当我 运行 它在交互式 shell 中它才有效,但如果 运行 来自詹金斯。

tst_subscriber = ["timeout", "-s", "KILL", str(timeout),"bash","-c", subsciber_executable]
tst_publisher = ["timeout","-s", "KILL", str(timeout),"bash","-c", publisher_executable]
kill_publisher = lambda process: subprocess.call(['docker-runc', 'delete', pub_name, "-f"])
kill_subscriber = lambda process: subprocess.call(['docker-runc', 'delete', sub_name, "-f"])

test_env = os.environ.copy()
# workaround for buffering problem which causes no captured output for python subprocesses
test_env["PYTHONUNBUFFERED"] = "1"

sub_pro = Popen(tst_subscriber, stdout=subprocess.PIPE, env=test_env)
pub_pro = Popen(tst_publisher, stdout=subprocess.PIPE, env=test_env)

timeout_sub = Timer(timeout, kill_subscriber, [sub_pro])
timeout_pub = Timer(timeout, kill_publisher, [pub_pro])

timeout_sub.start()
timeout_pub.start()
(output, err) = sub_pro.communicate()

print "Subscriber stdout:"
print output
print "Subscriber stderr:"
print err

由于几个开放的 runc 问题 #1965 #1721,在 config.json 中设置 terminal: true 会导致管道困难。看到 terminal: false 解决了问题。