Windows 7 64bit上的Python子进程-stdout = PIPE时无输出 [英] Python subprocess on Windows 7 64bit - no output when stdout=PIPE

查看:119
本文介绍了Windows 7 64bit上的Python子进程-stdout = PIPE时无输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于另一个关于Python子进程的问题表示歉意,但我找不到这个问题的答案.

Apologies for another question about Python subprocesses, but I couldn't find an answer to this one.

我在一些Python代码上遇到了麻烦,这些代码在Windows 7 64位系统上调用了子进程.当子进程的标准输出发送到管道时,不会产生任何输出.子进程似乎可以正常运行和终止,只是不产生任何输出.

I am having trouble with some Python code which calls a subprocess on Windows 7 64-bit. When the subprocess's stdout is sent to a pipe, no output is produced. The subprocess appears to run and terminate without problems, it just doesn't produce any output.

相同的代码在WinXP 32bit上可以正常工作,所以我更新了问题标题.

The same code works correctly on WinXP 32bit, so I updated the question title.

# (listing 1)
from subprocess import *
#cmdline= (a valid command line)
proc = Popen(cmdline,shell=True,stdout=PIPE,stderr=PIPE)
out, err = proc.communicate()
print( out )
print( err )

这给出了输出

out:

err:

但是,当子过程的输出未通过管道传输时,它会按预期产生输出:

However, when the subprocess's output is not piped, it produces output as expected:

# (listing 2)
proc = Popen(cmdline,shell=True)
proc.communicate()

这会将预期的输出提供给控制台.

This gives the expected output to console.

我相信可执行文件实际上正在将其输出写入stdout.我有C源代码,并添加了以下代码行:

I'm confident the executable is actually writing its output to stdout. I have the C source code, and I added the line:

fprintf(stdout, "HELLO");

同样,运行清单2而不是清单1时会看到"HELLO".

Again, "HELLO" is seen when running listing 2, but not listing 1.

我还尝试制作一个新的C ++可执行文件并从cmdline调用它:

I also tried making a new C++ executable and calling that from cmdline:

#include <iostream>

int main()
{
    std::cout << "HELLO" << std::endl;
}

同一件事仍然发生-运行清单2而不是清单1时会看到"HELLO".

The same thing still happens - "HELLO" is seen when running listing 2, but not listing 1.

如果将cmdline设置为'dir',则清单1和清单2都会发生预期的事情-目录内容会打印到控制台.

If I set cmdline to 'dir', the expected thing happens for both listing 1 and listing 2 - directory contents are printed to the console.

我尝试过的其他方法:Python 3.3和Python 2.7(相同的结果); bufsize = 0(结果相同);检查proc.returncode(为0,与预期的一样);删除stderr = PIPE(在这种情况下,清单1给出了预期的"err:None").

Other things I tried: Python 3.3 and Python 2.7 (same results); bufsize=0 (same results); checking proc.returncode (it's 0, as expected); removing stderr=PIPE (in which case listing 1 gives "err: None" as expected).

编辑-我也尝试了proc.stdout = proc.stdout,而不是具有相同结果的communication()方法. Python文档和其他问题表明,communication()方法是使用的正确方法.

EDIT - I also tried out = proc.stdout instead of the communicate() method with the same results. Python documentation and other questions suggest the communicate() method is the right one to use.

推荐答案

这根本不是Python问题.我的防火墙设置已将可执行文件沙盒化",这导致它们的输出被丢弃,而没有任何警告或错误.

It's not a Python problem at all. My firewall settings had "sandboxed" the executables, and this caused their output to be discarded without any kind of warning or error.

阻止它们执行将更有意义.我想我需要使用其他防火墙软件.

It would have made more sense to prevent them from executing. I think I need to use different firewall software.

这篇关于Windows 7 64bit上的Python子进程-stdout = PIPE时无输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆