代码之家  ›  专栏  ›  技术社区  ›  Edgar Navasardyan

在多线程环境下生成5xx错误报告

  •  0
  • Edgar Navasardyan  · 技术社区  · 7 年前

    我正在使用Sentry在我的应用程序中启动异常处理日志。

    问题出现在以下代码段中:

    @api_view(['POST'])
    def testView(request):
        a = 1/0 # This error is reported to Sentry
        TestThread().start()
        return f_response_ok()
    
    class TestThread(threading.Thread):
    
        def __init__(self, *args, **kwargs):
            super(TestThread, self).__init__(*args, **kwargs)
    
        def run(self):
            print('Test')
            a = 1/0 # but this one is not
            return True
    

    是否可以使哨兵报告在并行线程中发生的错误?

    有点离题: 如果有人对这种编程模式是否过时(应该改用rabbitmq之类的东西)给出简短的评论,我将不胜感激。

    1 回复  |  直到 7 年前
        1
  •  3
  •   Sardorbek Imomaliev    7 年前

    你可以手动记录到哨兵那里。

    https://docs.sentry.io/clients/python/#capture-an-error

    假设你用的是django

    from raven.contrib.django.raven_compat.models import client
    
    class TestThread(threading.Thread):
    
        def __init__(self, *args, **kwargs):
            super(TestThread, self).__init__(*args, **kwargs)
    
        def run(self):
            print('Test')
            try:
                a = 1/0 # error is not reported in Sentry
            except: # I would suggest putting here expected exceptions
                client.captureException()
            return True