简体   繁体   English

redis Python 捕获不继承自 BaseException 的异常

[英]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.

相关问题 无法捕获模拟异常,因为它不继承 BaseException - Can't catch mocked exception because it doesn't inherit BaseException Python 3 处理错误 TypeError:不允许捕获不从 BaseException 继承的类 - Python 3 handling error TypeError: catching classes that do not inherit from BaseException is not allowed 捕获不从Exception继承的异常 - Catching exceptions that don't inherit from Exception Paramiko 在使用“sftp.file/open”的脚本末尾引发“TypeError:不允许捕获不从 BaseException 继承的类” - Paramiko raising 'TypeError: catching classes that do not inherit from BaseException is not allowed' at the end of a script that uses `sftp.file/open` 为什么建议在Python中派生Exception而不是BaseException类? - Why is it recommended to derive from Exception instead of BaseException class in Python? SystemExit异常不视为源自BaseException - SystemExit exception not seen as derived from BaseException 为什么我不能继承Python中的dict和Exception? - Why can't I inherit from dict AND Exception in Python? 错误异常必须从 BaseException 派生,即使它确实存在(Python 2.7) - Error exception must derive from BaseException even when it does (Python 2.7) Python集合ValuesView abc:为什么它不继承Iterable? - Python collections ValuesView abc: why doesn't it inherit from Iterable? 不继承属性的Python子类 - Python subclass that doesn't inherit attributes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM