Python - Supervisor how to log the standard output -

Dario picture Dario · Sep 16, 2016 · Viewed 7.8k times · Source

I'm not expert about python, could someone explain where is the problem?

I'd like to collect the stdout through supervisor http://supervisord.org/

I've made 3 different scripts that print output, for bash and PHP I can collect the output, without problems, python doesn't work.

php_test.sh

#!/usr/bin/php
<?php
    for($i=0;$i<100000;$i++){
        sleep(5);
        echo 'test';
    }
?>

bash_test.sh

#!/bin/bash
for i in `seq 1 100000`; do
    sleep 5
    echo item: $i
done

python_test.sh ( with differents tests to print output )

#!/usr/bin/python3
import time
import sys
from contextlib import redirect_stdout

for num in range(0,100000):
    time.sleep(5)
    print("test!")
    sys.stdout.write("test")
    with redirect_stdout(sys.stderr):
        print("test")

My supervisor config files

bash

[program:bash_test]
process_name=bash_test
command=/home/user/bash_test.sh
stdout_logfile=/home/user/bash_test_output.log
stdout_logfile_maxbytes=0

php

[program:php_test]
process_name=php_test
command=/home/user/php_test.sh
stdout_logfile=/home/user/php_test_output.log
stdout_logfile_maxbytes=0

python

[program:python_test]
process_name=python_test
command=/home/user/python_test.sh
stdout_logfile=/home/user/python_test_output.log
stdout_logfile_maxbytes=0

Thank so much for the help. It's driving me crazy ;[

Answer

Dario picture Dario · Sep 16, 2016

Python output is buffered, use this after print

sys.stdout.flush()

or (Python 3)

print(something, flush=True)

or better

import logging
logging.warning('Watch out!')

https://docs.python.org/3/howto/logging.html