我刚接触Python和Web开发,有一个基础问题需要解决。
我使用Pyramid作为服务器,并使用nohup.out将输出写入文件。
nohup ../bin/pserve development.ini
当我执行tail -f nohup.out
命令时,
我可以看到从我的代码中的logging.info()
调用发出的所有输出,
但我没有看到所有print()
调用的输出。
这是什么原因,如何设置才能在nohup文件中看到print()的输出?
nohup python -u python_script.py &
nohup stdbuf -oL python python_script.py > nohup.out &
&
结束,对吗? - lleioustdbuf -oL
,行为都是相同的。 - jdhao当输出不是到终端时,会缓冲打印输出。 nohup
会将 stdout 替换为非终端(实际上是文件)。
如果没有任何代码,我的猜测是您的输出被缓冲在 Python 进程中,当缓冲区满时,它将被刷新到 nohup.out
。要确定是否是这种情况,请尝试添加更多的打印内容以加快填充缓冲区的速度。
-u: force the binary I/O layers of stdout and stderr to be unbuffered; stdin is always buffered; text I/O layer will be line-buffered; also PYTHONUNBUFFERED=x
- Logan Yangpython -u
对我有效,而stdbuf -oL -eL python
则无效。 - user2204107