【问题标题】:pipe python3 output into dzen2管道 python3 输出到 dzen2
【发布时间】:2014-05-01 12:43:39
【问题描述】:

我尝试将 python3 的一些输出通过管道传输到 dzen2,但 dzen 不会更新。

在 bash 中:

i=0; while true; do; echo $i; (( i++ )); sleep 1; done | dzen2

输出终端:

0
1
2
3...

在python中

import time
i=0
while True:
  print(i)
  i+=1
  time.sleep(1)

输出终端:

0
1
2
3...

python3 while.py |禅2 黑空dzen2-bar

bash-loop.sh |禅2 黑条从 0 计数到 ...

【问题讨论】:

  • 缓冲。缓冲。缓冲。
  • 当 python 的输出到 tty 时,它会缓冲 stdout。但是当它的输出是一个管道时,stdout 是块缓冲的。这意味着 python 会保存所有数据,直到它有一个完整的块(1024 字节,或 512 或 4192,取决于您的系统)要写入。这是非常标准的。见:stackoverflow.com/questions/107705/python-output-buffering
  • @WilliamPursell:请将评论转换为答案。

标签: python linux pipe


【解决方案1】:

当 python 的输出到 tty 时,它会缓冲 stdout。但是当它的输出是管道时,stdout 是块缓冲的。这意味着 python 会保存所有数据,直到它有一个完整的块(1024 字节,或 512 或 4192,取决于您的系统)要写入。这是非常标准的。请参阅:stackoverflow.com/questions/107705/python-output-buffering

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-04
    • 2013-05-25
    相关资源
    最近更新 更多