Django中Celery执行定时通知任务触发ValueError解包错误
Django + Celery 调度任务执行报错:ValueError: not enough values to unpack (expected 3, got 0)
我在Django项目中使用Celery调度通知任务,启动Celery Worker后任务运行时出现如下错误:
ValueError: not enough values to unpack (expected 3, got 0)
完整回溯信息:
File "celery\app\trace.py", line 640, in fast_trace_task tasks, accept, hostname = _loc ValueError: not enough values to unpack (expected 3, got 0)
触发错误的任务代码(core/tasks.py):
from celery import shared_task from django.utils import timezone from core.models import Notification @shared_task def send_scheduled_notifications(): now = timezone.now() notifications = Notification.objects.filter( is_read=False, send_at__lte=now ) for notification in notifications: print(f"Sending notification: {notification.title}") notification.is_read = True notification.save()
Celery启动成功且显示任务已接收,但随即因上述错误崩溃。
环境信息
- Windows 11
- Python 3.13
- Celery 5.5.1
- 本地运行Redis
我已确认该任务在Celery外手动执行完全正常。
我遇到过类似问题,但我的错误发生在不同版本的Celery中,且任务结构看似正确。
解决方案
1. 版本兼容性排查
Python 3.13属于较新版本,Celery 5.5.1对其适配可能存在问题:
- 降级Python到3.12版本(Celery 5.5.x官方明确支持的稳定版本)
- 升级Celery至最新测试版,确认是否修复了Python 3.13的兼容问题
2. 验证任务注册配置
确保Celery正确加载任务模块:
在项目根目录的celery.py中检查自动发现任务的配置:
import os from celery import Celery os.environ.setdefault('DJANGO_SETTINGS_MODULE', '你的项目名.settings') app = Celery('你的项目名') app.config_from_object('django.conf:settings', namespace='CELERY') # 显式指定任务所在app,或使用autodiscover_tasks()自动扫描所有app app.autodiscover_tasks(['core'])
3. Windows环境特殊处理
Windows运行Celery需规避多进程问题:
- 启动Worker时添加
--pool=solo参数:
celery -A 你的项目名 worker -l info --pool=solo
- 确认Redis连接配置正确(
settings.py中):
CELERY_BROKER_URL = 'redis://localhost:6379/0' CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'
4. 清除任务缓存
缓存的任务注册信息可能引发异常:
- 清空Redis对应数据库的缓存数据
- 重启Redis服务和Celery Worker
内容的提问来源于stack exchange,提问作者Google User
相关产品推荐
相关产品推荐

