代码之家  ›  专栏  ›  技术社区  ›  Santi

从无限期运行的popen获取部分stdout和stderr[duplicate]

  •  2
  • Santi  · 技术社区  · 15 年前

    可能重复:
    Bypassing buffering of subprocess output with popen in C or Python

    我正在为服务器命令行脚本构建一个包装器,该脚本应该无限期运行。 我需要做的是在不等待子进程完成的情况下获取当前的stout。

    我是说,如果我运行以下命令,一切正常:

    ls = Popen(["ls"], stdout=PIPE)
    output = ls.stdout.read()
    

    但如果我对一个不确定运行的程序也这么做:

    server = Popen(["python","-m","SimpleHTTPServer"], stdout=PIPE)
    output = server.stdout.read()
    

    它不会再回来了…

    更新:甚至

     output = server.stdout.read(1) 
    

    挂起…

    你知道有没有一种方法可以以独立于操作系统的方式从popen(或类似的线程实现)捕获部分输出吗?

    1 回复  |  直到 8 年前
        1
  •  -1
  •   sean riley    15 年前

    我猜read()会返回整个内容?如果在循环中读取固定大小的块,可能会得到更好的结果。