首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >python popen,stdout在strace中显示,但不在popen.stdout.read()中显示。

python popen,stdout在strace中显示,但不在popen.stdout.read()中显示。
EN

Stack Overflow用户
提问于 2018-12-20 04:44:40
回答 1查看 198关注 0票数 0

我有下面的python脚本,我用它来尝试从一个没有适当换行符的程序中读取所有内容。这允许读取,而不必担心阻塞读取。然而,由于我对线程的了解不足,我怀疑这就是我的问题所在。

代码语言:javascript
复制
import subprocess
import shlex
import os
import time
from threading import Thread
import queue


class NonBlockingStreamReader:
    def __init__(self, stream):
        """
        :param stream: the stream to read from.  Usually stdout or stderr.
        """
        self._s = stream
        self._q = queue.Queue()

        def _populate_queue(_stream, _queue):
            """Collect lines from 'stream' and put them in 'queue'"""
            while True:
                _char = _stream.read(1)
                if _char:
                    _queue.put(_char)
                else:
                    raise UnexpectedEndOfStream

        self._t = Thread(
            target=_populate_queue,
            args=(
                self._s,
                self._q
            )
        )
        self._t.daemon = True
        self._t.start() # Start collecting characters from the stream

    def readchar(self, timeout=None):
        try:
            _tmp = self._q.get(block=timeout is not None, timeout=timeout)
            return _tmp
        except queue.Empty:
            return None


class UnexpectedEndOfStream(Exception):
    pass


def main():
    proc = subprocess.Popen(
        shlex.split('strace -o /home/arts/dlm/trace_output.txt stdbuf -o0 /home/arts/dlm/test'),
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )

    nbsr = NonBlockingStreamReader(proc.stdout)

    _data = b''
    while True:
        _char = nbsr.readchar(0.1)
        if not _char:
            break
        else:
            _data += _char
    print(_data.decode())

    proc.stdin.write(b'12345\n')

    _data = b''
    while True:
        _char = nbsr.readchar(5)
        if not _char:
            break
        else:
            _data += _char
    print(_data.decode())
    print('Annnnd done.')


if __name__ == '__main__':
    main()

下面是test程序的预期输出:

代码语言:javascript
复制
Line 1 test
Line 2 test
Line 3 input: 12345      <--- input from user
Line 4 test: 12345

下面是strace的输出:

代码语言:javascript
复制
write(1, "Line 1 test", 11)             = 11
write(1, "\n", 1)                       = 1
write(1, "Line 2 test", 11)             = 11
write(1, "\n", 1)                       = 1
write(1, "Line 3 input: ", 14)          = 14
fstat(0, {st_mode=S_IFIFO|0600, st_size=0, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fb76716d000
read(0, "12345\n", 4096)                = 6
write(1, "Line 4 test: 12345\n\n", 20)  = 20
exit_group(1)                           = ?

这表明(至少对我而言)应用程序提供了请求的输出。除非我遗漏了什么,否则read(0, "12345\n", 4096)会显示proc.stdin.write(b'12345\n')。下一行显示它读回了我期望的输出。然而,实际的输出是:

代码语言:javascript
复制
Line 1 test
Line 2 test
Line 3 input:

Annnnd done.

如果我将一些打印语句放在_char = _stream.read(1)后面,它什么也不会显示。如果我将它们添加到readchar函数中,则会显示None

有些东西破坏了stdout,所以无论它去哪里,它都不会进入管道。有人能给我指引正确的方向吗?

EN

回答 1

Stack Overflow用户

发布于 2018-12-20 04:56:07

我想通了。需要将bufsize=0作为arg转到Popen

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53858892

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档