PM2不记录Python3打印语句 [英] PM2 doesn't log Python3 print statements

查看:180
本文介绍了PM2不记录Python3打印语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PM2在后台运行Python程序

I'm using PM2 to run a Python program in the background like so

pm2 start helloworld.py

,并且效果很好.但是,在helloworld.py中,我有几个用作日志的打印语句.例如,当出现网络请求或数据库值已更新时.当我像这样运行helloworld.py时:

and it works perfectly fine. However, within helloworld.py I have several print statements that act as logs. For example, when a network request comes in or if a database value is updated. When I run helloworld.py like so:

python3 helloworld.py

所有这些打印语句都是可见的,我可以调试我的应用程序.但是,运行时

all these print statements are visible and I can debug my application. However, when running

pm2 logs helloworld

这些打印语句均未显示.

none of these print statements show up.

推荐答案

这个问题已有几个月的历史了,所以也许您在不久前就发现了这个问题,但是当我遇到相同的问题时,它是Google热门搜索之一问题,所以我想我要添加发现的内容.

This question is a few months old, so maybe you figured this out a while ago, but it was one of the top google hits when I was having the same problem so I thought I'd add what I found.

似乎是python如何缓冲sys.stdout的问题.在某些平台/实例中,当由pm2或nohup调用时,sys.stdout流可能不会刷新,直到进程退出.将"-u"参数传递给python解释器可阻止其缓冲sys.stdout.在pm2的process.json文件中,我添加了"interpreter_args":-u",现在我可以正常获取日志了.

Seems like it's an issue with how python buffers sys.stdout. In some platforms/instances, when called by say pm2 or nohup, the sys.stdout stream may not get flushed until the process exits. Passing the "-u" argument to the python interpreter stops it from buffering sys.stdout. In the process.json for pm2 I added "interpreter_args": "-u" and I'm getting logs normally now.

这篇关于PM2不记录Python3打印语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆