[fix] No event loop is set and defensive programming is not used for non-main thread calls.

This commit is contained in:
lanceyq
2026-03-31 09:59:39 +08:00
parent abc27c8372
commit 2d6cde157e

View File

@@ -50,7 +50,12 @@ def get_thread_safe_redis() -> redis.StrictRedis:
or getattr(_thread_local, "pid", None) != current_pid \
or loop_stale:
_thread_local.pid = current_pid
_thread_local.loop = asyncio.get_event_loop()
# Python 3.10+: get_event_loop() raises RuntimeError in threads
# where no loop has been set yet (e.g. Celery --pool=threads).
try:
_thread_local.loop = asyncio.get_event_loop()
except RuntimeError:
_thread_local.loop = None
_thread_local.pool = ConnectionPool.from_url(
_REDIS_URL,
db=settings.REDIS_DB,