python - 使用管道时子进程调用不会终止

发布于 2024-10-09 06:18:41 字数 1670 浏览 3 评论 0原文

我对 python 2.6.5 有一个奇怪的问题。如果我

p = subprocess.Popen(["ifup eth0"], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()

在 eth0 接口关闭的情况下调用,python 程序就会挂起。 “p.communicate()”需要一分钟或更长时间才能完成。如果之前接口已经打开,则程序运行顺利。我从命令行手动测试了“ifup eth0”这两种情况,并且速度快如闪电。

如果您知道问题可能是什么,我将非常感激。

预先感谢

编辑:

根据答案,我尝试了以下操作:

p = subprocess.Popen(["ifup", "eth0"], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()

如果界面之前已启动,则脚本运行顺利。但是,如果接口关闭,Python 会再次挂起。我也尝试过:

p = subprocess.Popen(["ifup", "eth0"], shell=False)
out, err = p.communicate()

一切都运行得非常快。因此,正如 funktku 所指出的那样,它可能确实与僵局有关。然而,python 文档还说 python ref

警告

使用时会出现死锁 stdout=PIPE 和/或 stderr=PIPE 和 子进程产生足够的输出 到一个管道,使其阻塞等待 让操作系统管道缓冲区接受更多 数据。使用communicate()来避免这种情况。

因此不应该出现僵局。嗯...以下是我在命令行上运行程序时的详细输出:

1 Case,接口 eth0 已启动:

ifup eth0
Interface eth0 already configured

2 Case,接口之前已关闭:

ifup eth0 
ssh stop/waiting
ssh start/running

因此 ifup 命令会生成两行输出,以防接口之前已关闭否则输出一行。这是我注意到的唯一区别。但我怀疑这就是问题的原因,因为“ls -ahl”会导致更多的输出行并且运行得很好。

我还尝试使用 buffersize 参数,但是没有成功,将其设置为像 4096 这样的大值。

您有什么想法吗?这可能是什么原因?或者这可能是 python Pipe 处理或 ifup 命令本身的错误?我真的必须使用旧的 os.popen(cmd).read() 吗???

EDIT2:

os.popen(cmd).read() 也遇到同样的问题。知道如何在命令行上测试 ifup 的管道行为吗?

我很感激每一个提示,提前致谢

I have a strange issue with python 2.6.5. If I call

p = subprocess.Popen(["ifup eth0"], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()

with the interface eth0 being down, the python programm hangs. "p.communicate()" takes a minute or longer to finish. If the interface is up before, the programm runs smoothly. I tested "ifup eth0" from the command line manually for both cases and its lightning fast.

If you have any idea what the Problem might be, I would appreciate it very much.

Thanks in advance

EDIT:

Based on the answers, I tried the following things:

p = subprocess.Popen(["ifup", "eth0"], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()

If the interface was up before, the skript runs smoothly. However if the interface was down, python hangs again. I also tried:

p = subprocess.Popen(["ifup", "eth0"], shell=False)
out, err = p.communicate()

And EVERYTHING runs perfektly fast. Therefore it might be indeed related to a deadlock, as pointed out funktku. However the python documentation also says python ref:

Warning

This will deadlock when using
stdout=PIPE and/or stderr=PIPE and the
child process generates enough output
to a pipe such that it blocks waiting
for the OS pipe buffer to accept more
data. Use communicate() to avoid that.

Therefore there shouldn't be a deadlock. Hmm... Here is the detailed output when I run the programms on the command line:

1 Case, interface eth0 already up:

ifup eth0
Interface eth0 already configured

2 Case, interface down before:

ifup eth0 
ssh stop/waiting
ssh start/running

So the ifup command generates two lines of ouput in case the interface is down before and one line of ouput otherwise. This is the only difference I noticed. But I doubt this is the cause of the problem, since "ls -ahl" causes many more lines of ouput and is running very well.

I also tried playing around with the buffersize argument, however no success, by setting it to some large value like 4096.

Do you have an ideas, what might be the cause of that? Or is this probably a bug in python Pipe handling or the ifup command itself? Do I really have to use the old os.popen(cmd).read()????

EDIT2:

os.popen(cmd).read() suffers from the same problem. Any idea of how I can test the pipe behaviour of ifup on the commandline?

I appreciate every hint, thanks in advance

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

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

发布评论

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

评论(3

完美的未来在梦里 2024-10-16 06:18:44

/etc/network/if-up.d/ntpdate 未正确分离< /a>

这就是 read() 等待 fds (stdin/stdout/stderr) 关闭的原因。
您可以分离 stdin/stderr/stdout (不要添加 stdout=subprocess.PIPE 以及与 Popen 构造函数调用相同的内容)。

/etc/network/if-up.d/ntpdate does not detach correctly

That's why read() waits until fds (stdin/stdout/stderr) are closed.
You can detach stdin/stderr/stdout (do not add stdout=subprocess.PIPE and the same to Popen constructor call).

余生再见 2024-10-16 06:18:44
p = subprocess.Popen(["ifup", "eth0"], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()

设置shell=False,你不需要它。

尝试运行这段代码,它应该可以工作。请注意两个参数如何成为列表中的单独元素。

p = subprocess.Popen(["ifup", "eth0"], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()

Set shell=False, you don't need it.

Try running this code, it should work. Notice how two arguments are separate elements in the list.

ˇ宁静的妩媚 2024-10-16 06:18:43

您应该查看 subprocess.call< 下的 警告 /代码> 方法。这可能是你的问题的原因。

警告

像 Popen.wait() 一样,这将
使用 stdout=PIPE 和/或时出现死锁
stderr=PIPE 和子进程
生成足够的输出到管道,例如
它会阻塞等待操作系统管道
缓冲区以接受更多数据。

You should check out the warning under the subprocess.call method. It might be the reason of your problem.

Warning

Like Popen.wait(), this will
deadlock when using stdout=PIPE and/or
stderr=PIPE and the child process
generates enough output to a pipe such
that it blocks waiting for the OS pipe
buffer to accept more data.

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