gpt4 book ai didi

django - ConnectionAbortedError - 如果 Celery 停止/失败或 Redis 未启动。我如何捕获/除此错误/

转载 作者:IT王子 更新时间:2023-10-29 06:02:14 25 4
gpt4 key购买 nike

我有一个与 Redis 一起工作的 Celery 任务:

@app.task(bind=True, name='task_a', max_retries=4, soft_time_limit_exception=300)
def task_a(self, a, b):
try:
# some code here
except Exception as e:
raise self.retry(exc=e, countdown=exponential_backoff(self))


def exponential_backoff(task_self):
minutes = task_self.default_retry_delay / 60
rand = random.uniform(minutes, minutes * 1.5)
return int(rand ** task_self.request.retries) * 60

我有以下问题:

如果 Redis 不工作或 celery worker 停止/失败,代码将“等待”而不是继续执行代码。

如果我将 try except block 放在任务中,或者在 try except 中调用的任务,它什么也抓不到,我得到一个 ConnectionAbortedError。

在这种情况下,我想要的是捕获此特定错误并执行其他操作,或者忽略而不调用重试。


@Pavel Minenkov 回答建议使用 sentry.io,但问题是我不知道要捕获它。


查看回溯:

----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 49301)
Traceback (most recent call last):
File "D:\DevEnv\Python\lib\wsgiref\handlers.py", line 138, in run
self.finish_response()
File "D:\DevEnv\Python\lib\wsgiref\handlers.py", line 180, in finish_response
self.write(data)
File "D:\DevEnv\Python\lib\wsgiref\handlers.py", line 274, in write
self.send_headers()
File "D:\DevEnv\Python\lib\wsgiref\handlers.py", line 332, in send_headers
self.send_preamble()
File "D:\DevEnv\Python\lib\wsgiref\handlers.py", line 255, in send_preamble
('Date: %s\r\n' % format_date_time(time.time())).encode('iso-8859-1')
File "D:\DevEnv\Python\lib\wsgiref\handlers.py", line 453, in _write
result = self.stdout.write(data)
File "D:\DevEnv\Python\lib\socketserver.py", line 796, in write
self._sock.sendall(b)
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "D:\DevEnv\Python\lib\wsgiref\handlers.py", line 141, in run
self.handle_error()
File "D:\DevEnv\PythonVENV\DjangoDev\lib\site-packages\django\core\servers\basehttp.py", line 86, in handle_error
super().handle_error()
File "D:\DevEnv\Python\lib\wsgiref\handlers.py", line 368, in handle_error
self.finish_response()
File "D:\DevEnv\Python\lib\wsgiref\handlers.py", line 180, in finish_response
self.write(data)
File "D:\DevEnv\Python\lib\wsgiref\handlers.py", line 274, in write
self.send_headers()
File "D:\DevEnv\Python\lib\wsgiref\handlers.py", line 331, in send_headers
if not self.origin_server or self.client_is_modern():
File "D:\DevEnv\Python\lib\wsgiref\handlers.py", line 344, in client_is_modern
return self.environ['SERVER_PROTOCOL'].upper() != 'HTTP/0.9'
TypeError: 'NoneType' object is not subscriptable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "D:\DevEnv\Python\lib\socketserver.py", line 647, in process_request_thread
self.finish_request(request, client_address)
File "D:\DevEnv\Python\lib\socketserver.py", line 357, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "D:\DevEnv\Python\lib\socketserver.py", line 717, in __init__
self.handle()
File "D:\DevEnv\PythonVENV\DjangoDev\lib\site-packages\django\core\servers\basehttp.py", line 154, in handle
handler.run(self.server.get_app())
File "D:\DevEnv\Python\lib\wsgiref\handlers.py", line 144, in run
self.close()
File "D:\DevEnv\Python\lib\wsgiref\simple_server.py", line 35, in close
self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'

最佳答案

首先实现监控系统。我喜欢哨兵:https://sentry.io/welcome/ 1 位用户免费

from raven import Client
...
@app.task(bind=True, name='task_a', max_retries=4, soft_time_limit_exception=300)
def task_a(self, a, b):
sentry = Client(dsn=<your>)
with sentry.capture_exceptions():
# some code here

然后您将能够找出引发的异常,然后您将能够解决问题

关于django - ConnectionAbortedError - 如果 Celery 停止/失败或 Redis 未启动。我如何捕获/除此错误/,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51454403/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com