• <small id='tlo5g'></small><noframes id='tlo5g'>

    <tfoot id='tlo5g'></tfoot>

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

      • <bdo id='tlo5g'></bdo><ul id='tlo5g'></ul>

    2. <legend id='tlo5g'><style id='tlo5g'><dir id='tlo5g'><q id='tlo5g'></q></dir></style></legend>

        子进程标准输入缓冲区未在 bufsize=1 的换行符上刷新

        时间:2023-07-21

              <legend id='V0rsR'><style id='V0rsR'><dir id='V0rsR'><q id='V0rsR'></q></dir></style></legend>
                <tbody id='V0rsR'></tbody>
              <tfoot id='V0rsR'></tfoot>

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

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

                  本文介绍了子进程标准输入缓冲区未在 bufsize=1 的换行符上刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有两个小的 python 文件,第一个使用 input 读取一行,然后打印另一行

                  I have two small python files, the first reads a line using input and then prints another line

                  a = input()
                  print('complete')
                  

                  第二次尝试将其作为子进程运行

                  The second attempts to run this as a subprocess

                  import subprocess
                  
                  proc = subprocess.Popen('./simp.py',
                                          stdout=subprocess.PIPE,
                                          stdin=subprocess.PIPE,
                                          bufsize=1)
                  print('writing')
                  proc.stdin.write(b'hey
                  ')
                  print('reading')
                  proc.stdout.readline()
                  

                  上面的脚本会先打印writing"然后是reading",然后挂起.起初我认为这是一个标准输出缓冲问题,所以我将 bufsize=1 更改为 bufsize=0,这确实解决了问题.但是,似乎是标准输入导致了问题.

                  The above script will print "writing" then "reading" but then hang. At first I thought this was a stdout buffering issue, so I changed bufsize=1 to bufsize=0, and this does fix the problem. However, it seems it's the stdin that's causing the problem.

                  使用 bufsize=1,如果我在写入下方添加 proc.stdin.flush(),该过程将继续.这两种方法看起来都很笨拙,因为(1)无缓冲的流很慢(2)到处添加刷新容易出错.为什么上面的 write 不在换行符上刷新?文档说在为子进程创建标准输入、标准输出和标准错误流时使用了 bufsize,那么是什么导致写入不刷新在换行符上?

                  With bufsize=1, if I add proc.stdin.flush() below the write, the process continues. Both of these approaches seem clumsy since (1) unbuffered streams are slow (2) adding flushes everywhere is error-prone. Why does the above write not flush on a newline? The docs say that bufsize is used when creating stdin, stdout, and stderr stream for the subprocess, so what's causing the write to not flush on the newline?

                  推荐答案

                  来自 文档:1 表示行缓冲(仅在universal_newlines=True 时可用,即在文本模式下)".这有效:

                  import subprocess
                  
                  proc = subprocess.Popen('./simp.py',
                                          stdout=subprocess.PIPE,
                                          stdin=subprocess.PIPE,
                                          bufsize=1,
                                          universal_newlines=True)
                  
                  print('writing')
                  proc.stdin.write('hey
                  ')
                  print('reading')
                  proc.stdout.readline()
                  

                  这篇关于子进程标准输入缓冲区未在 bufsize=1 的换行符上刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为什么向 subprocess.Popen 提供标准输入会导致写入标准输出的内容发生变化? 下一篇:与 popen python 一起使用时,输入命令似乎不起作用

                  相关文章

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

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

                    1. <tfoot id='FXVJ9'></tfoot>