無限ループを使用して Web サーバーと通信する Python スクリプトを作成しました。すべての通信データをファイルに記録し、同時にターミナルから監視したいので、次のように tee コマンドを使用しました。
python client.py | tee logfile
しかし、ターミナルやログファイルからは何も得られませんでした。Python スクリプトは正常に動作しています。何が起こっているのでしょうか?
ベストアンサー1
から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