【问题标题】:How can I print the traceback and exception of a zmq.REQ worker?如何打印 zmq.REQ 工作人员的回溯和异常?
【发布时间】:2018-12-14 02:51:46
【问题描述】:

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

但我发现zmq.REQ worker 的回溯和异常不会在我运行master.py (zmq.REP) 的终端中打印出来。

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

【问题讨论】:

    标签: python pexpect sys


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 2012-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-19
      • 1970-01-01
      • 2016-06-18
      • 1970-01-01
      • 2017-12-21
      相关资源
      最近更新 更多