我制作了一个 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/