subprocess.Popen behavior after cx_freeze

馋奶兔 提交于 2019-12-07 19:49:43

问题


I have some python code using subprocess.Popen to open a console application and get stdout/stderr from it.

Launching from the interpreter works fine and as intended.

After using cx_freeze with --base-name Win32GUI option the Popen pops up in a console window now and I can't capture stdout/stderr. If I remove --base-name Win32GUI it works as intended but I now have a console behind the UI.

Here is the code (I've tried it without startupinfo and without shell=False):

startupinfo = subprocess.STARTUPINFO()
            startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
            startupinfo.wShowWindow = subprocess.SW_HIDE
subprocess.Popen(['exe', 'arg1', 'arg2'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False, startupinfo=startupinfo)

I'm using out, err = p.communicate() to grab stdout/stderr


回答1:


Okay I found a solution. It looks like since its a windows GUI Application stdout handle doesn't exist and it looks like subprocess inherits that behavior. So a workaround is a simple one and a more complicated one involved with the win32api and creating a pipe for it (didn't try this method).

Here is what finally worked:

startupinfo = subprocess.STARTUPINFO()
            startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
            startupinfo.wShowWindow = subprocess.SW_HIDE
stdout_file = tempfile.NamedTemporaryFile(mode='r+', delete=False)
process = subprocess.Popen(['exe', 'arg1', 'arg2'], stdin=subprocess.PIPE, stdout=stdout_file, stderr=subprocess.PIPE, shell=False, startupinfo=startupinfo)
return_code = process.wait()
stdout_file.flush()
stdout_file.seek(0) # This is required to reset position to the start of the file
out = stdout_file.read()
stdout_file.close()


来源:https://stackoverflow.com/questions/24151539/subprocess-popen-behavior-after-cx-freeze

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!