[英]redis Python catching exception that doesn't inherit from BaseException
We're seeing exceptions in our log like the following:我们在日志中看到如下异常:
ERROR Exception ignored in: <function Connection.__del__ at 0x7f9b70a5cc20>
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.7/site-packages/redis/connection.py", line 537, in __del__
File "/app/.heroku/python/lib/python3.7/site-packages/redis/connection.py", line 667, in disconnect
TypeError: catching classes that do not inherit from BaseException is not allowed
According to the Redis source code, the offending line is the except
in the following snippet:根据 Redis 源代码,有问题的行是以下代码段中的except
:
try:
if os.getpid() == self.pid:
shutdown(self._sock, socket.SHUT_RDWR)
self._sock.close()
except socket.error:
pass
Which would indicate that socket.exception doesn't inherit from BaseException.这表明 socket.exception 不是从 BaseException 继承的。 However, as far as I can tell (based on the docs and the mro
class method), socket.exception does inherit from BaseException.但是,据我所知(基于文档和mro
class 方法),socket.exception确实继承自 BaseException。
Why is this happening?为什么会这样? What can I do to prevent it?我能做些什么来防止它?
By the way, our code doesn't call Redis directly.顺便说一句,我们的代码没有直接调用 Redis。 We are using Redis Queue (rq), which is implemented using Redis.我们正在使用 Redis 队列 (rq),它是使用 Redis 实现的。
This would happen if you didn't close the redis client explicitly.如果您没有明确关闭 redis 客户端,就会发生这种情况。 That's why you saw __del__
in the traceback.这就是您在回溯中看到__del__
的原因。
I'm not using rq but I'll take celery as an example, yet the idea could also be applied to rq.我没有使用 rq,但我会以 celery 为例,但这个想法也可以应用于 rq。
# tasks/__init__.py
from celeryapp import redis_client
@worker_shutdown.connect # this signal means it's about to shut down the worker
def cleanup(**kwargs):
redis_client.close() # without this you may see error
# celeryapp.py
from celery import Celery
import redis
app = Celery(config_source="celeryconfig")
redis_client = redis.StrictRedis()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.