如何打印zmq.REQ工作程序的回溯和异常?

时间:2018-12-14 02:51:47

标签: python pexpect sys

我正在使用pexpect.spawn编写一些可以并行运行的脚本。

但是我发现在运行zmq.REQmaster.py)的终端中不会打印出zmq.REP工作人员的回溯和异常。

我知道sys.stderr可用于重定向回溯和异常,但是我不知道如何在worker.py中使用它,以便可以在worker.py中发生异常打印出来。

1 个答案:

答案 0 :(得分:1)

使用logging.exception并登录到文件。

示例:

import logging

logging.basicConfig(filename='example.log') 


def fail():
    return 10/0

try:
    fail()
except Exception as err:
    loggin.exception(err)

输出(example.log):

ERROR:root:integer division or modulo by zero
Traceback (most recent call last):
  File "<ipython-input-4-d63f4b56d991>", line 2, in <module>
    fail()
  File "<ipython-input-3-edce7c179301>", line 2, in fail
    return 10/0
ZeroDivisionError: integer division or modulo by zero
相关问题