简体   繁体   English

Windows 7 64位上的Python子进程 - 当stdout = PIPE时没有输出

[英]Python subprocess on Windows 7 64bit - no output when stdout=PIPE

Apologies for another question about Python subprocesses, but I couldn't find an answer to this one. 对于另一个关于Python子进程的问题抱歉,但我找不到这个问题的答案。

I am having trouble with some Python code which calls a subprocess on Windows 7 64-bit. 我遇到一些Python代码,它在Windows 7 64位上调用子进程。 When the subprocess's stdout is sent to a pipe, no output is produced. 当子进程的stdout发送到管道时,不会产生任何输出。 The subprocess appears to run and terminate without problems, it just doesn't produce any output. 子进程似乎运行和终止没有问题,它只是不产生任何输出。

EDIT: The same code works correctly on WinXP 32bit, so I updated the question title. 编辑:相同的代码在WinXP 32位上正常工作,所以我更新了问题标题。

# (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 )

This gives output 这给出了输出

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. 这为控制台提供了预期的输出。

I'm confident the executable is actually writing its output to stdout. 我确信可执行文件实际上是将其输出写入stdout。 I have the C source code, and I added the line: 我有C源代码,我添加了这一行:

fprintf(stdout, "HELLO");

Again, "HELLO" is seen when running listing 2, but not listing 1. 同样,在运行清单2时会看到“HELLO”,但不会列出清单1。

I also tried making a new C++ executable and calling that from cmdline: 我还尝试制作一个新的C ++可执行文件并从cmdline调用它:

#include <iostream>

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

The same thing still happens - "HELLO" is seen when running listing 2, but not listing 1. 同样的事情仍然发生 - 运行清单2时会看到“HELLO”,但不会列出清单1。

If I set cmdline to 'dir', the expected thing happens for both listing 1 and listing 2 - directory contents are printed to the console. 如果我将cmdline设置为'dir',则列表1和列表2都会出现预期的情况 - 目录内容将打印到控制台。

Other things I tried: Python 3.3 and Python 2.7 (same results); 我尝试过的其他事情:Python 3.3和Python 2.7(结果相同); bufsize=0 (same results); bufsize = 0(相同的结果); checking proc.returncode (it's 0, as expected); 检查proc.returncode(它是0,如预期的那样); removing stderr=PIPE (in which case listing 1 gives "err: None" as expected). 删除stderr = PIPE(在这种情况下,列表1按预期给出“err:None”)。

EDIT - I also tried out = proc.stdout instead of the communicate() method with the same results. 编辑 - 我也尝试了= proc.stdout而不是带有相同结果的communic()方法。 Python documentation and other questions suggest the communicate() method is the right one to use. Python文档和其他问题表明,communication()方法是正确的使用方法。

It's not a Python problem at all. 这根本不是Python的问题。 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. 我想我需要使用不同的防火墙软件。

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

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