简体   繁体   English

子进程Popen不使用pythonw.exe

[英]Subprocess Popen not working with pythonw.exe

I want to be able to get the contents of stdout and stderr when I run the following script on windows using pythonw.exe: 我希望能够在使用pythonw.exe在Windows上运行以下脚本时获取stdout和stderr的内容:

import subprocess
import sys
import os
import string
import time

tmpdir = 'c:/temp'
cmd = 'dir c:'

tmpfile = "tmp_%f" % (time.time())
tmpfile = os.path.normpath(os.path.join(tmpdir,tmpfile))
tmpfile2 = tmpfile+".bat"
tmpfile3 = tmpfile+".txt"

fa = open(tmpfile2,'w')
fa.write("@ECHO OFF > NUL\n")
fa.write('call '+cmd+"\n")
fa.close()

wcmd = []
wcmd.append(tmpfile2)

startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess._subprocess.STARTF_USESHOWWINDOW

fb = open(tmpfile3,'w')
fb.write("\n")
fb.write(tmpfile2+"\n")

try:
    procval = subprocess.Popen(wcmd, startupinfo=startupinfo, stdout=subprocess.PIPE, stderr=subprocess.STDOUT).communicate()
    fb.write(str(procval)+"\n")
    fb.write("Sucess")
    fb.close()
except:
    fb.write(str(procval)+"\n")
    fb.write("Failure")
    fb.close()

When I execute it using python.exe I get the expected output. 当我使用python.exe执行它时,我得到了预期的输出。 When I run it using pythonw.exe I end up on the exception side. 当我使用pythonw.exe运行它时,我最终在异常端。 If I run the popen with just the command and the startupinfo flags the command will successfully complete but no access to the data in the child processs. 如果我只使用命令和startupinfo标志运行popen,命令将成功完成,但无法访问子进程中的数据。 Everything that I read stated that this should work but must be missing something. 我读到的所有内容都表明这应该有效但必须遗漏一些东西。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Thanks, Randy 谢谢,兰迪

This is possibly a bug when using pythonw.exe 使用pythonw.exe时,这可能是一个错误

pythonw.exe starts a daemon process which doesn't have the normal access to the standard file descriptors. pythonw.exe启动守护进程,该进程无法正常访问标准文件描述符。 The only thing you would need to do in your script is to specifically set the 3rd fd for stdin: 您在脚本中唯一需要做的就是专门为stdin设置第3个fd:

p = subprocess.Popen(wcmd,
                     startupinfo=startupinfo,
                     stdout=subprocess.PIPE,
                     stderr=subprocess.STDOUT,
                     stdin=subprocess.PIPE)
procval = p.communicate()

(A few years late with this answer...) I've run across the same problem and found that you should replace this: (这个答案迟了几年......)我遇到了同样的问题,发现你应该替换它:

stdout=subprocess.PIPE 标准输出= subprocess.PIPE

with this: 有了这个:

stdin=subprocess.PIPE 标准输入= subprocess.PIPE

Here's my guess at the underlying problem: pythonw launches the process with no stdin (makes sense, since there is no console). 这是我对潜在问题的猜测:pythonw启动没有标准输入的进程(有意义,因为没有控制台)。 The sub-process tries to access stdin and this causes an error. 子进程尝试访问stdin,这会导致错误。

An alternative is to use /dev/null for stdin, see this thread for a discussion on how to do that: Ignoring output from subprocess.Popen (except you'll want to use devnull on stdin). 另一种方法是对stdin使用/ dev / null,请参阅此线程以讨论如何执行此操作: 忽略 subprocess.Popen的输出 (除非您要在stdin上使用devnull)。

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

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