1

我有 2 个文件 main.py 和 infinit.py,如下所示:

主文件

#!/usr/bin/python

import logging
import subprocess
import sys

logging.basicConfig(level=logging.INFO)


def forever():
    cmd = [sys.executable, 'infinit.py']
    while 1:
        try:
            print 'running new instance of:'
            print ' '.join(cmd)
            popen = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                stderr=subprocess.PIPE, universal_newlines=True)

            for line in iter(popen.stderr.readline, ""):
                print line,
            for line in iter(popen.stdout.readline, ""):
                print line,

        except Exception as e:
            print 'Something bad happend'
            logging.error(e)


if __name__ == '__main__':
    forever()

无限.py

#!/usr/bin/python

import logging

logging.basicConfig(level=logging.INFO)

i = 0
while 1:
    i += 1
    logging.info('i: {0}'.format(i))
    print i

我运行 main.py 并且我想在我的控制台中同时查看(打印和日志记录)。我也希望它在 windows 和 linux 上运行。此外,它是否可以在 Windows Idle 中工作(打印和记录)?

4

2 回答 2

0

在程序的请求处添加这一行,并放入相同的LOG_FILENAME,然后您可以使用tail -f unix 命令查看输出。

import logging
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG, filemode = 'w+')
logging.getLogger().setLevel(logging.DEBUG)
于 2012-10-29T16:45:39.690 回答
0

我找到了两种解决方案,它们都应该适用于 Linux 和 Windows,甚至适用于 Windows Idle:

  1. 更简单的:

在父进程中打印子进程的 stderr:

#!/usr/bin/python

import logging
import subprocess
import sys

def forever():
    cmd = [sys.executable, 'infinit.py']
    while 1:
        try:
            print 'running new instance of:'
            print ' '.join(cmd)
            popen = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                stderr=subprocess.PIPE, universal_newlines=True)

            for line in iter(popen.stderr.readline, ""):
                print line,

        except Exception as e:
            print 'Something bad happend'
            logging.error(e)

if __name__ == '__main__':
    forever()

目前:

  • 父打印子日志(因为默认情况下日志将消息发送到我们正在打印的标准错误和标准错误)
  • 父母不打印孩子的印刷品,所以:

无限.py

#!/usr/bin/python

import logging
import sys
import time

logging.basicConfig(level=logging.INFO)

sys.stdout = sys.stderr

i = 0
while 1:
    i += 1
    logging.info('i: {0}'.format(i))
    print i
    time.sleep(0.2)
  1. 另一种方法在这里:

在运行时拦截子进程的标准输出

我的代码适应了解决方案

主文件

#!/usr/bin/python

import logging
import subprocess
import sys

def forever():
    CMD = [sys.executable, 'infinit.py']
    while 1:
        try:
            print 'running new instance of:'
            print ' '.join(CMD)

            popen = subprocess.Popen(CMD, stdout=subprocess.PIPE)
            for line in iter(popen.stdout.readline, ""):
                print line,

        except Exception as e:
            print 'Something bad happend'
            logging.error(e)
        finally:
            print


if __name__ == '__main__':
    forever()

初始化文件

#!/usr/bin/python

import logging
import sys
import time


class FlushFile(object):
    """Write-only flushing wrapper for file-type objects."""
    def __init__(self, f):
        self.f = f
    def write(self, x):
        self.f.write(x)
        self.f.flush()

logging.basicConfig(level=logging.INFO)

sys.stdout = FlushFile(sys.stdout)


i = 0
while 1:
    i += 1
    logging.info('i: {0}'.format(i))
    print i
    time.sleep(0.2)
于 2012-10-30T14:04:09.887 回答