简体   繁体   English

Python subprocess.Popen :使用 shell=True 运行多个命令,但它似乎没有等待所有命令完成

[英]Python subprocess.Popen : Running multiple commands with shell=True, but it doesn't seem to be waiting for all commands to complete

I'm trying to use subprocess.Popen to run a check on Kafka consumer groups and log their state, but it doesn't appear to be waiting for all the commands to run.我正在尝试使用 subprocess.Popen 对 Kafka 消费者组进行检查并记录他们的状态,但它似乎没有等待所有命令运行。 It isn't giving me any stdout, but its also returning an exit code of 0.它没有给我任何标准输出,但它也返回 0 的退出代码。

prompt = ["cd", "~/path/to/kafka_2.11-2.1.0;", "pwd;", "./bin/kafka-consumer-groups.sh", 
          "--bootstrap-server", "localhost:9092", "--describe", "--group", "groupname"]

response = subprocess.run(prompt, stdout=subprocess.PIPE, stderr=subprocess.PIPE, 
                          shell=True, check=True)
print(response)

Prints:印刷:

CompletedProcess(args=['cd', '~/path/to/kafka_2.11-2.1.0;', 'pwd;', './bin/kafka-consumer-groups.sh', '--bootstrap-server', 'localhost:9092', '--describe', '--group', 'groupname'], returncode=0, stdout=b'', stderr=b'')

The pwd command was to primarily test if it would return any kind of stout, it won't be kept. pwd 命令主要是测试它是否会返回任何类型的粗壮,它不会被保留。

I've looked through the docs for subprocess, and I haven't see anything that suggests that it is unable to capture multiple stdouts.我已经查看了子流程的文档,但没有看到任何表明它无法捕获多个标准输出的内容。 Also, according to the logs, the CompletedProcess is returned in less than 10ms, while running cd ~path/to/kafka_2.11-2.1.0; pwd; ./bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group groupname此外,根据日志,在运行cd ~path/to/kafka_2.11-2.1.0; pwd; ./bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group groupname ,CompletedProcess 在不到 10 毫秒内返回cd ~path/to/kafka_2.11-2.1.0; pwd; ./bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group groupname cd ~path/to/kafka_2.11-2.1.0; pwd; ./bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group groupname cd ~path/to/kafka_2.11-2.1.0; pwd; ./bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group groupname takes about 10-15s on my machine. cd ~path/to/kafka_2.11-2.1.0; pwd; ./bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group groupname在我的机器上大约需要 10-15 秒。

Please note that I'm using python3.5.2请注意,我使用的是 python3.5.2

I found my error.我发现了我的错误。 cd doesn't work with subprocess, but subprocess offers the cwd named argument that accepts the path you need to run arguments. cd不适用于子进程,但子进程提供 cwd 命名参数,该参数接受运行参数所需的路径。 The reason it was returning a CompletedProcess so quickly is that it was successfully changing directories and then exiting the subprocess.它如此快速地返回 CompletedProcess 的原因是它成功地更改了目录,然后退出了子进程。

Sorry, I wasn't thinking about cd being the culprit until I came across this question which answered my problem too.抱歉,在我遇到这个问题也回答了我的问题之前,我没想到 cd 是罪魁祸首。

Your error is more fundamental than you think.你的错误比你想象的更根本。 Your code runs你的代码运行

sh -c 'cd'

with $0 set to the directory, $1 set to pwd; $0设置为目录, $1设置为pwd; , etc; , 等等; so very far from what you want.所以从远你想要什么。 (Just cd simply switches to your home directory; then the shell exits, without doing anything with all those arguments you passed in, and Python continues back in whichever directory was current before you launched the subprocess.) (只需cd简单地切换到您的主目录;然后 shell 退出,对您传入的所有参数不做任何处理,Python 继续返回您启动子进程之前的当前目录。)

Generally, pass a single string as the first argument with shell=True , and a list of strings when you don't have a shell.通常,使用shell=True传递单个字符串作为第一个参数,当您没有 shell 时传递一个字符串列表。

subprocess.run(r"cd foo; pwd; use shell commands to your heart\'s content; run as many processes as you like as subprocesses of your shell subprocess", shell=True)
subprocess.run(['/bin/echo', 'one', 'single', 'process', 'with', 'arguments])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM