如何打印zmq.REQ worker的回溯和异常?

2024-03-29 08:46:20 发布

您现在位置:Python中文网/ 问答频道 /正文

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

但是我发现zmq.REQ工作者的回溯和异常不会在运行master.pyzmq.REP)的终端中打印出来

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


Tags: pymaster脚本终端stderrsyszmqreq
1条回答
网友
1楼 · 发布于 2024-03-29 08:46:20

使用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

相关问题 更多 >