【问题标题】:Python - Supervisor how to log the standard output -Python - 主管如何记录标准输出 -
【发布时间】:2017-01-24 15:05:00
【问题描述】:

我不是python方面的专家,谁能解释一下问题出在哪里?

我想通过主管收集标准输出 http://supervisord.org/

我制作了 3 个不同的脚本来打印输出,对于 bash 和 PHP,我可以收集输出,没有问题,python 不起作用。

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(使用不同的测试来打印输出)

#!/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")

我的主管配置文件

狂欢

[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

蟒蛇

[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

非常感谢您的帮助。 快把我逼疯了;[

【问题讨论】:

  • 两个输出都不起作用?
  • PHP 和 BASH 工作正常,Python 不打印任何内容。如果我使用python3 ./python_test.sh 执行脚本,我可以在控制台中看到输出,但在主管日志中看不到;(

标签: python linux python-3.x unix supervisord


【解决方案1】:

Python 输出被缓冲,打印后使用它

sys.stdout.flush()

或(Python 3)

print(something, flush=True)

或更好

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

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

【讨论】:

    【解决方案2】:

    您还可以使用pip install supervisor-stdout 安装标准输出友好版本。找到使用说明here

    更新: 您还可以更新您的supervisord.conf 以将输出指向标准输出。

    [program:worker2]
    command=bash yourscript.sh
    stdout_logfile=/dev/stdout
    stdout_logfile_maxbytes=0
    

    【讨论】:

      猜你喜欢
      • 2016-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-22
      • 1970-01-01
      • 2016-09-24
      相关资源
      最近更新 更多