<tfoot id='NP63G'></tfoot>
      <bdo id='NP63G'></bdo><ul id='NP63G'></ul>

      <legend id='NP63G'><style id='NP63G'><dir id='NP63G'><q id='NP63G'></q></dir></style></legend>
    1. <small id='NP63G'></small><noframes id='NP63G'>

    2. <i id='NP63G'><tr id='NP63G'><dt id='NP63G'><q id='NP63G'><span id='NP63G'><b id='NP63G'><form id='NP63G'><ins id='NP63G'></ins><ul id='NP63G'></ul><sub id='NP63G'></sub></form><legend id='NP63G'></legend><bdo id='NP63G'><pre id='NP63G'><center id='NP63G'></center></pre></bdo></b><th id='NP63G'></th></span></q></dt></tr></i><div id='NP63G'><tfoot id='NP63G'></tfoot><dl id='NP63G'><fieldset id='NP63G'></fieldset></dl></div>

      使用 pythonw.exe 时 Python subprocess.call() 失败

      Python subprocess.call() fails when using pythonw.exe(使用 pythonw.exe 时 Python subprocess.call() 失败)
      <i id='IzyZf'><tr id='IzyZf'><dt id='IzyZf'><q id='IzyZf'><span id='IzyZf'><b id='IzyZf'><form id='IzyZf'><ins id='IzyZf'></ins><ul id='IzyZf'></ul><sub id='IzyZf'></sub></form><legend id='IzyZf'></legend><bdo id='IzyZf'><pre id='IzyZf'><center id='IzyZf'></center></pre></bdo></b><th id='IzyZf'></th></span></q></dt></tr></i><div id='IzyZf'><tfoot id='IzyZf'></tfoot><dl id='IzyZf'><fieldset id='IzyZf'></fieldset></dl></div>
      • <small id='IzyZf'></small><noframes id='IzyZf'>

          <tfoot id='IzyZf'></tfoot>
            <bdo id='IzyZf'></bdo><ul id='IzyZf'></ul>

              <legend id='IzyZf'><style id='IzyZf'><dir id='IzyZf'><q id='IzyZf'></q></dir></style></legend>
                  <tbody id='IzyZf'></tbody>
                本文介绍了使用 pythonw.exe 时 Python subprocess.call() 失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一些 Python 代码在我使用 python.exe 运行时可以正常工作,但如果我使用 pythonw.exe 会失败.

                <上一页>def runStuff(命令行):outputFileName = 'somefile.txt'输出文件 = 打开(输出文件名,w")尝试:结果 = subprocess.call(commandLine, shell=True, stdout=outputFile)除了:print '抛出异常:', str(sys.exc_info()[1])myThread = threading.Thread(None, target=runStuff, commandLine=['whatever...'])myThread.start()

                我得到的信息是:

                <上一页>抛出异常:[错误 6] 句柄无效

                但是,如果我不指定 'stdout' 参数,subprocess.call() 启动正常.

                我可以看到 pythonw.exe 可能正在重定向输出本身,但我不明白为什么我无法为新线程指定 stdout.

                解决方案

                sys.stdinsys.stdout 句柄无效,因为 pythonw 不提供控制台支持作为守护进程运行,因此 subprocess.call() 的默认参数失败.

                Deamon 程序故意关闭 stdin/stdout/stderr 并改用日志记录,因此您必须自己管理:我建议使用 subprocess.PIPE.

                如果您真的不关心子进程对错误的说明以及所有内容,您可以使用 os.devnull(我不太确定可移植性如何是吗?)但我不建议这样做.

                I have some Python code that works correctly when I use python.exe to run it, but fails if I use pythonw.exe.

                    def runStuff(commandLine):
                        outputFileName = 'somefile.txt'
                        outputFile = open(outputFileName, "w")
                
                        try:
                            result = subprocess.call(commandLine, shell=True, stdout=outputFile)
                        except:
                            print 'Exception thrown:', str(sys.exc_info()[1])
                
                    myThread = threading.Thread(None, target=runStuff, commandLine=['whatever...'])
                    myThread.start()
                

                The message I get is:

                    Exception thrown: [Error 6] The handle is invalid
                

                However, if I don't specify the 'stdout' parameter, subprocess.call() starts okay.

                I can see that pythonw.exe might be redirecting output itself, but I can't see why I'm blocked from specifying stdout for a new thread.

                解决方案

                sys.stdin and sys.stdout handles are invalid because pythonw does not provide console support as it runs as a deamon, so default arguments of subprocess.call() are failing.

                Deamon programs close stdin/stdout/stderr purposedly and use logging instead, so that you have to manage this yourself: I would suggest to use subprocess.PIPE.

                If you really don't care about what the sub process says for errors and all, you could use os.devnull (I'm not really sure how portable it is?) but I wouldn't recommend that.

                这篇关于使用 pythonw.exe 时 Python subprocess.call() 失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
                Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
                Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
                Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
                Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
                Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)

                <small id='QMZZO'></small><noframes id='QMZZO'>

                  • <legend id='QMZZO'><style id='QMZZO'><dir id='QMZZO'><q id='QMZZO'></q></dir></style></legend>
                      <tbody id='QMZZO'></tbody>
                        <bdo id='QMZZO'></bdo><ul id='QMZZO'></ul>
                      • <tfoot id='QMZZO'></tfoot>

                          <i id='QMZZO'><tr id='QMZZO'><dt id='QMZZO'><q id='QMZZO'><span id='QMZZO'><b id='QMZZO'><form id='QMZZO'><ins id='QMZZO'></ins><ul id='QMZZO'></ul><sub id='QMZZO'></sub></form><legend id='QMZZO'></legend><bdo id='QMZZO'><pre id='QMZZO'><center id='QMZZO'></center></pre></bdo></b><th id='QMZZO'></th></span></q></dt></tr></i><div id='QMZZO'><tfoot id='QMZZO'></tfoot><dl id='QMZZO'><fieldset id='QMZZO'></fieldset></dl></div>