我有以下代码:
def save()
super().save(*args, **kwargs)
try:
transaction.on_commit(lambda: c_task.delay(a, b, self.pk))
except Exception as e:
print(e)
@app.task(bind=True, name='c_task', max_retries=4, soft_time_limit_exception=300)
def c_task(self, a, b, i):
from .models import ModelA
try:
json_data = entity(a,b,i, const)
.....
except Exception as e:
raise self.retry(exc=e, countdown=exponential_backoff(self))
如果 Redis 服务器出现故障,我会收到和操作错误。我尝试像以前一样使用芹菜操作错误或通用异常来捕获它。
该行仍在执行并引发错误。如果 Redis 失败,我不希望一切都失败,因为我的代码有一个"解决方法",但我无法捕获错误。
追踪:
transaction.on_commit(lambda: c_task.delay(a, b, self.pk))
libsite-packagesceleryapptask.py in delay
return self.apply_async(args, kwargs)
libsite-packagesceleryapptask.py in apply_async
**options
libsite-packagesceleryappbase.py in send_task
amqp.send_task_message(P, name, message, **options)
libcontextlib.py in __exit__
self.gen.throw(type, value, traceback)
libsite-packageskombuconnection.py in _reraise_as_library_errors
sys.exc_info()[2])
libsite-packagesvinefive.py in reraise
raise value.with_traceback(tb)
libsite-packageskombuconnection.py in _reraise_as_library_errors
yield
libsite-packagesceleryappbase.py in send_task
self.backend.on_task_call(P, task_id)
libsite-packagescelerybackendsredis.py in on_task_call
self.result_consumer.consume_from(task_id)
libsite-packagescelerybackendsredis.py in consume_from
return self.start(task_id)
libsite-packagescelerybackendsredis.py in start
self._consume_from(initial_task_id)
libsite-packagescelerybackendsredis.py in _consume_from
self._pubsub.subscribe(key)
libsite-packagesredisclient.py in subscribe
ret_val = self.execute_command('SUBSCRIBE', *iterkeys(new_channels))
libsite-packagesredisclient.py in execute_command
self._execute(connection, connection.send_command, *args)
libsite-packagesredisclient.py in _execute
connection.connect()
dlibsite-packagesredisconnection.py in connect
raise ConnectionError(self._error_message(e))
您需要在下面进行更改
transaction.on_commit(lambda: c_task.delay(a, b, self.pk))
自
def run_task(a, b, pk):
try:
c_task.delay(a, b, pk)
except Exception as ex:
print(ex)
transaction.on_commit(lambda: run_task(a, b, self.pk))
这将确保处理连接异常