1

我正在编写一个 Python 命令行程序。

有一个主要的 Python 脚本文件,作为入口点。当用户运行此脚本时,它将执行一些外部 Python 脚本文件。外部 Python 脚本文件也可以执行其他外部 Python 脚本文件。外部文件的数量是可变的。

Python 脚本将使用以下命令执行外部 Python 脚本:

p = subprocess.Popen(args)

or

p = subprocess.call(args)

当我在终端窗口中运行主要的 Python 脚本时,它会在运行时在屏幕上打印实时日志消息。现在,我想从主 Python 脚本调用的所有外部 Python 脚本中获取所有日志消息,并将其打印到同一个终端窗口(我用来运行主脚本的终端窗口)。

例如,下面是脚本执行的顺序:

1.Main-script
    |
    2.Layer-1-script-1
        |
        3.Layer-2-script-1
        |
        4.Layer-2-script-2
        |
    5.Layer-1-script-2
    |
    6.Layer-1-script-3
    |
7.Main-script(continued)

当我在终端窗口中运行主脚本时,是否可以在终端窗口上获得实时日志消息,如下所示?

[time-hh-mm-ss][log message from main script]Script is running..
[time-hh-mm-ss][log message from main script]Calling script layer-1-script-1..
[time-hh-mm-ss][log message from layer-1-script-1]Script is running..
[time-hh-mm-ss][log message from layer-1-script-1]Calling script layer-2-script-1..
[time-hh-mm-ss][log message from layer-2-script-1]Script is running..
[time-hh-mm-ss][log message from layer-2-script-1]Calling script layer-2-script-2..
[time-hh-mm-ss][log message from layer-2-script-2]Script is running..
[time-hh-mm-ss][log message from layer-2-script-2]Calling script layer-1-script-2..
[time-hh-mm-ss][log message from layer-1-script-2]Script is running..
[time-hh-mm-ss][log message from layer-1-script-2]Calling script layer-1-script-3..
[time-hh-mm-ss][log message from layer-2-script-3]Script is running..
[time-hh-mm-ss][log message from main script]Back to main script. Script is running..

是否有可能real time log messages在终端窗口中得到类似的结果?

4

2 回答 2

0

如果你依赖subprocess模块,那么你只能通过subprocess.PIPEs 进行通信,以便从子进程中收集标准输出和标准错误。因此,让子进程将其日志输出写入 stdout/err 并通过管道将子进程连接到父进程。通过这样做,您基本上可以将子进程的标准输出打印到父进程的标准输出。

于 2012-09-13T15:05:48.980 回答
0

如果您可以控制所有 Python 脚本,那么您可以使用multiprocessing而不是subprocess使用 which 您可以执行以下操作:

测试.py

import logging
import test2
import multiprocessing as mp

logger = mp.get_logger()

def main():
    logger.info('Script is running')
    logger.info('Calling script test2')
    proc = mp.Process(target = test2.main)
    proc.start()
    proc.join()

if __name__ == '__main__':
    formatter = logging.Formatter('[%(asctime)s] [%(filename)s]: %(message)s',
                                  datefmt = '%H:%M:%S')
    handler = logging.StreamHandler()
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    logger.setLevel(logging.INFO)    
    main()

测试2.py:

import multiprocessing as mp

logger = mp.get_logger()

def main():
    logger.info('Script is running...')

运行test.py产量

[11:36:50] [test.py]: Script is running
[11:36:50] [test.py]: Calling script test2
[11:36:50] [util.py]: child process calling self.run()
[11:36:50] [test2.py]: Script is running...
[11:36:50] [util.py]: process shutting down
[11:36:50] [util.py]: process exiting with exitcode 0
[11:36:50] [util.py]: process shutting down
于 2012-09-13T15:07:09.797 回答