Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PM2 doesn't log Python3 print statements

Tags:

python

pm2

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

pm2 start 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.

like image 247
Carpetfizz Avatar asked Jun 22 '16 05:06

Carpetfizz


1 Answers

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.

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.

like image 180
HigherAbstraction Avatar answered Nov 13 '22 09:11

HigherAbstraction