<bdo id='L1pdG'></bdo><ul id='L1pdG'></ul>

    1. <small id='L1pdG'></small><noframes id='L1pdG'>

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

      <tfoot id='L1pdG'></tfoot>

      为什么 shell=True 会吃掉我的 subprocess.Popen 标准输出?

      时间:2023-09-02

    2. <small id='j4P43'></small><noframes id='j4P43'>

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

            • <tfoot id='j4P43'></tfoot>
              • 本文介绍了为什么 shell=True 会吃掉我的 subprocess.Popen 标准输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                似乎在链的第一个进程中使用 shell=True 会以某种方式从下游任务中删除标准输出:

                It seems that using shell=True in the first process of a chain somehow drops the stdout from downstream tasks:

                p1 = Popen(['echo','hello'], stdout=PIPE)
                p2 = Popen('cat', stdin=p1.stdout, stdout=PIPE)
                p2.communicate()
                # outputs correctly ('hello
                ', None)
                

                让第一个进程使用 shell=True 会以某种方式杀死输出...

                Making the first process use shell=True kills the output somehow...

                p1 = Popen(['echo','hello'], stdout=PIPE, shell=True)
                p2 = Popen('cat', stdin=p1.stdout, stdout=PIPE)
                p2.communicate()
                # outputs incorrectly ('
                ', None)
                

                shell=True 在第二个进程上似乎并不重要.这是预期的行为吗?

                shell=True on the second process doesn't seem to matter. Is this expected behavior?

                推荐答案

                当你传递 shell=True 时,Popen 需要一个字符串参数,而不是一个列表.所以当你这样做时:

                When you pass shell=True, Popen expects a single string argument, not a list. So when you do this:

                p1 = Popen(['echo','hello'], stdout=PIPE, shell=True)
                

                会发生什么:

                execve("/bin/sh", ["/bin/sh", "-c", "echo", "hello"], ...)
                

                也就是说,它调用 sh -c echo",而 hello 被有效地忽略(从技术上讲,它成为 shell 的位置参数).所以 shell 运行 echo,它打印 ,这就是为什么你会在输出中看到它.

                That is, it calls sh -c "echo", and hello is effectively ignored (technically it becomes a positional argument to the shell). So the shell runs echo, which prints , which is why you see that in your output.

                如果你使用shell=True,你需要这样做:

                If you use shell=True, you need to do this:

                p1 = Popen('echo hello', stdout=PIPE, shell=True)
                

                这篇关于为什么 shell=True 会吃掉我的 subprocess.Popen 标准输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:python子进程交互,为什么我的进程使用Popen.communicate,而不使用Popen.stdout.read 下一篇:使用 subprocess.Popen 调用 python 脚本并刷新数据

                相关文章

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

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

              • <legend id='Msfwl'><style id='Msfwl'><dir id='Msfwl'><q id='Msfwl'></q></dir></style></legend>