Python popen 命令。等待命令完成

发布于 2024-09-02 06:16:56 字数 169 浏览 3 评论 0原文

我有一个脚本,可以使用 popen shell 命令启动。 问题是脚本不会等到 popen 命令完成并立即继续。

om_points = os.popen(command, "w")
.....

如何告诉我的 Python 脚本等待 shell 命令完成?

I have a script where I launch with popen a shell command.
The problem is that the script doesn't wait until that popen command is finished and go continues right away.

om_points = os.popen(command, "w")
.....

How can I tell to my Python script to wait until the shell command has finished?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

葬花如无物 2024-09-09 06:16:57

wait() 对我来说效果很好。子进程p1、p2和p3同时执行。因此,所有过程在3秒后完成。

import subprocess

processes = []

p1 = subprocess.Popen("sleep 3", stdout=subprocess.PIPE, shell=True)
p2 = subprocess.Popen("sleep 3", stdout=subprocess.PIPE, shell=True)
p3 = subprocess.Popen("sleep 3", stdout=subprocess.PIPE, shell=True)

processes.append(p1)
processes.append(p2)
processes.append(p3)

for p in processes:
    if p.wait() != 0:
        print("There was an error")

print("all processed finished")

wait() works fine for me. The subprocesses p1, p2 and p3 are executed at the same. Therefore, all processes are done after 3 seconds.

import subprocess

processes = []

p1 = subprocess.Popen("sleep 3", stdout=subprocess.PIPE, shell=True)
p2 = subprocess.Popen("sleep 3", stdout=subprocess.PIPE, shell=True)
p3 = subprocess.Popen("sleep 3", stdout=subprocess.PIPE, shell=True)

processes.append(p1)
processes.append(p2)
processes.append(p3)

for p in processes:
    if p.wait() != 0:
        print("There was an error")

print("all processed finished")
只有影子陪我不离不弃 2024-09-09 06:16:57

我认为 process.communicate() 适合小尺寸的输出。对于更大的输出,这不是最好的方法。

I think process.communicate() would be suitable for output having small size. For larger output it would not be the best approach.

莫言歌 2024-09-09 06:16:56

根据您想要如何运行脚本,您有两种选择。如果您希望命令在执行时阻塞并且不执行任何操作,则可以使用 subprocess.call

#start and block until done
subprocess.call([data["om_points"], ">", diz['d']+"/points.xml"])

如果您想在执行时执行操作或将内容输入到 stdin 中,则可以在 popen 调用之后使用 communicate

#start and process things, then wait
p = subprocess.Popen([data["om_points"], ">", diz['d']+"/points.xml"])
print "Happens while running"
p.communicate() #now wait plus that you can send commands to process

如文档中所述,wait 可能会死锁,因此建议进行通信。

Depending on how you want to work your script you have two options. If you want the commands to block and not do anything while it is executing, you can just use subprocess.call.

#start and block until done
subprocess.call([data["om_points"], ">", diz['d']+"/points.xml"])

If you want to do things while it is executing or feed things into stdin, you can use communicate after the popen call.

#start and process things, then wait
p = subprocess.Popen([data["om_points"], ">", diz['d']+"/points.xml"])
print "Happens while running"
p.communicate() #now wait plus that you can send commands to process

As stated in the documentation, wait can deadlock, so communicate is advisable.

落日海湾 2024-09-09 06:16:56

您可以使用subprocess来实现这一点。

import subprocess

#This command could have multiple commands separated by a new line \n
some_command = "export PATH=$PATH://server.sample.mo/app/bin \n customupload abc.txt"

p = subprocess.Popen(some_command, stdout=subprocess.PIPE, shell=True)

(output, err) = p.communicate()  

#This makes the wait possible
p_status = p.wait()

#This will give you the output of the command being executed
print "Command output: " + output

You can you use subprocess to achieve this.

import subprocess

#This command could have multiple commands separated by a new line \n
some_command = "export PATH=$PATH://server.sample.mo/app/bin \n customupload abc.txt"

p = subprocess.Popen(some_command, stdout=subprocess.PIPE, shell=True)

(output, err) = p.communicate()  

#This makes the wait possible
p_status = p.wait()

#This will give you the output of the command being executed
print "Command output: " + output
锦爱 2024-09-09 06:16:56

通过执行以下操作,强制 popen 在读取所有输出之前不继续:

os.popen(command).read()

Force popen to not continue until all output is read by doing:

os.popen(command).read()
凶凌 2024-09-09 06:16:56

让您尝试传递的命令为,

os.system('x')

然后将其隐藏为一条语句,

t = os.system('x')

现在Python将等待命令行的输出,以便将其分配给变量t

Let the command you are trying to pass be

os.system('x')

then you covert it to a statement

t = os.system('x')

now the python will be waiting for the output from the commandline so that it could be assigned to the variable t.

涙—继续流 2024-09-09 06:16:56

您正在寻找的是 wait 方法。

What you are looking for is the wait method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文