python - linux tee 不能与 python 一起使用?

标签 python linux tee

我制作了一个 python 脚本,它使用无限循环与 Web 服务器进行通信。 我想将每个通信数据记录到一个文件中,并同时从终端监视它们。所以我使用了这样的 tee 命令。

python client.py | tee logfile

但是,我没有从终端或日志文件中得到任何信息。 python脚本工作正常。 这里发生了什么? 我错过了什么吗?

我们将不胜感激。 提前谢谢你。

最佳答案

来自man python:

   -u     Force stdin, stdout and stderr to  be  totally  unbuffered.   On  systems
          where it matters, also put stdin, stdout and stderr in binary mode.  Note
          that there is internal buffering in xreadlines(), readlines()  and  file-
          object  iterators  ("for  line  in sys.stdin") which is not influenced by
          this option.  To work around this, you will want to use  "sys.stdin.read‐
          line()" inside a "while 1:" loop.

所以你可以做的是:

/usr/bin/python -u client.py >> logfile 2>&1

或者使用tee:

python -u client.py | tee logfile

关于python - linux tee 不能与 python 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21662783/

相关文章:

linux - Lua 库需要编译时支持兼容

linux - Linux 应用程序如何确定标准输出是终端文件还是普通文件

linux - 按时间顺序捕获 STDOUT 和 STDERR

MySQL tee (\T) 命令不输出我的查询或评论

python - 非法指令 : 4 when importing tensorflow in python 3. 6

python - 连接两个字典并处理其共同值(value)

将文本从终端复制到内存

python - 使用 itertools.tee 检查下一个元素时如何最小化空间成本?

python - 将一系列中的最后一个值投影到 Python 中的 future 单元格

python - 根据值计数列删除排序行