<tfoot id='V1I8i'></tfoot>
    • <bdo id='V1I8i'></bdo><ul id='V1I8i'></ul>

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

      我可以在 Python 中将 io.BytesIO() 流通过管道传输到 subprocess.popen() 吗?

      Can I pipe a io.BytesIO() stream to subprocess.popen() in Python?(我可以在 Python 中将 io.BytesIO() 流通过管道传输到 subprocess.popen() 吗?)

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

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

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

            1. <legend id='XnkD7'><style id='XnkD7'><dir id='XnkD7'><q id='XnkD7'></q></dir></style></legend>
                <tbody id='XnkD7'></tbody>
                本文介绍了我可以在 Python 中将 io.BytesIO() 流通过管道传输到 subprocess.popen() 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试使用 subprocess.popen()io.BytesIO() 字节流管道 到一个单独的程序,但我不知道如何或是否有可能.文档和示例都是关于文本和换行符的.

                I'm trying to pipe a io.BytesIO() bytetream to a separate program using subprocess.popen(), but I don't know how or if this is at all possible. Documentation and examples are all about text and newlines.

                当我做出这样的事情时:

                When I whip up something like this:

                import io
                from subprocess import *
                
                stream = io.BytesIO()
                someStreamCreatingProcess(stream)
                
                command = ['somecommand', 'some', 'arguments']  
                process = Popen(command, stdin=PIPE)
                process.communicate(input=stream)
                

                我明白了

                Traceback (most recent call last):
                  File "./test.py", line 9, in <module>
                    procOut         = process.communicate(input=stream)
                  File "/usr/lib/python2.7/subprocess.py", line 754, in communicate
                    return self._communicate(input)
                  File "/usr/lib/python2.7/subprocess.py", line 1322, in _communicate
                    stdout, stderr = self._communicate_with_poll(input)
                  File "/usr/lib/python2.7/subprocess.py", line 1384, in _communicate_with_poll
                    chunk = input[input_offset : input_offset + _PIPE_BUF]
                TypeError: '_io.BytesIO' object has no attribute '__getitem__'
                

                我认为 popen() 仅适用于文本.我错了吗?
                有没有其他方法可以做到这一点?

                I think popen() is only for text. Am I wrong?
                Is there a different way to do this?

                推荐答案

                正如 @falsetru 所说你不能流式传输BytesIO() 对象直接;您需要先从中获取一个字节串.这意味着所有内容都应该已经写入 stream 你调用 stream.getvalue() 传递给 process.communicate().

                As @falsetru said you can't stream BytesIO() object directly; you need to get a bytestring from it first. It implies that all content should be already written to stream before you call stream.getvalue() to pass to process.communicate().

                如果你想 stream 而不是一次提供所有输入,那么你可以删除 BytesIO() 对象并直接写入管道:

                If you want to stream instead of providing all input at once then you could drop BytesIO() object and write to the pipe directly:

                from subprocess import Popen, PIPE
                
                process = Popen(['command', 'arg1'], stdin=PIPE, bufsize=-1)
                someStreamCreatingProcess(stream=process.stdin) # many `stream.write()` inside
                process.stdin.close() # done (no more input)
                process.wait()
                

                someStreamCreatingProcess() 在完成写入流之前不应返回.如果它立即返回,那么它应该在将来的某个时间调用 stream.close() (删除代码中的 process.stdin.close()):

                someStreamCreatingProcess() should not return until it is done writing to the stream. If it returns immediately then it should call stream.close() at some point in the future (remove process.stdin.close() in your code):

                from subprocess import Popen, PIPE
                
                process = Popen(['command', 'arg1'], stdin=PIPE, bufsize=-1)
                someStreamCreatingProcess(stream=process.stdin) # many `stream.write()` inside
                process.wait() # stream.close() is called in `someStreamCreatingProcess`
                

                这篇关于我可以在 Python 中将 io.BytesIO() 流通过管道传输到 subprocess.popen() 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                Pythonic and efficient way of finding adjacent cells in grid(在网格中查找相邻单元格的 Pythonic 和有效方法)
                map a hexagonal grid in matplotlib(在 matplotlib 中映射六边形网格)
                Execute arbitrary python code remotely - can it be done?(远程执行任意 python 代码 - 可以吗?)
                Python - Plotting colored grid based on values(Python - 根据值绘制彩色网格)
                Is there a GUI design app for the Tkinter / grid geometry?(是否有 Tkinter/网格几何图形的 GUI 设计应用程序?)
                tkinter Canvas Scrollbar with Grid?(带有网格的 tkinter 画布滚动条?)
                1. <i id='Ob1uA'><tr id='Ob1uA'><dt id='Ob1uA'><q id='Ob1uA'><span id='Ob1uA'><b id='Ob1uA'><form id='Ob1uA'><ins id='Ob1uA'></ins><ul id='Ob1uA'></ul><sub id='Ob1uA'></sub></form><legend id='Ob1uA'></legend><bdo id='Ob1uA'><pre id='Ob1uA'><center id='Ob1uA'></center></pre></bdo></b><th id='Ob1uA'></th></span></q></dt></tr></i><div id='Ob1uA'><tfoot id='Ob1uA'></tfoot><dl id='Ob1uA'><fieldset id='Ob1uA'></fieldset></dl></div>
                  <legend id='Ob1uA'><style id='Ob1uA'><dir id='Ob1uA'><q id='Ob1uA'></q></dir></style></legend>

                  • <tfoot id='Ob1uA'></tfoot>

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

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

                            <tbody id='Ob1uA'></tbody>