Python - 主管如何记录标准输出 -

标签 python linux python-3.x unix supervisord

我不是 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

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

非常感谢您的帮助。 这让我发疯;[

最佳答案

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

sys.stdout.flush()

或(Python 3)

print(something, flush=True)

或更好

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

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

关于Python - 主管如何记录标准输出 -,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39530567/

相关文章:

python - 如何优雅地找到 Python 枚举中的下一个和上一个值?

Python:列表的最长公共(public)子序列的长度

python - 在 Python 2.5 上可靠地终止子进程

Python进程和MySQL

python - 随机遍历 Python 中的二维列表

linux - .htaccess 重写重定向路径,但有异常(exception)

linux - 为什么试图终止 Docker 容器中的进程会让我退出?

python - 如何将目录导入为python模块

linux - 无法在 CentOS 上执行二进制文件 - Zabbix 外部脚本

json - 将 mongoengine 对象转换为 JSON