You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Celery Worker无法识别tasks.cache_popular_posts任务求助

解决Celery Beat传递任务给Worker时的KeyError问题

问题根源

Worker找不到名为tasks.cache_popular_posts的任务,原因是Django集成Celery时,自动发现的任务完整名称格式为应用名.任务模块.任务函数,而Beat配置中使用了不完整的短名称,导致Worker无法匹配。

修复步骤

  1. 修正Beat配置中的任务名称
    在app/celery.py的beat_schedule配置里,将任务名称改为完整的posts.tasks.cache_popular_posts:

    app.conf.beat_schedule = {
        'cache-popular-post&Meta-every-30-seconds': {
            'task': 'posts.tasks.cache_popular_posts',  # 修改此处
            'schedule': 30.0,
        },
    }
    
  2. 重启Celery服务
    停止当前运行的Worker和Beat进程,重新启动:

    # 重启Worker
    celery -A Pastebin_hardEdition worker -l info
    # 重启Beat
    celery -A Pastebin_hardEdition beat -l info
    
  3. 验证任务注册情况
    执行以下命令查看Worker已注册的任务列表,确认posts.tasks.cache_popular_posts存在:

    celery -A Pastebin_hardEdition inspect registered
    

额外检查点

  • 确保posts/apps.py中的ready方法正确导入了tasks模块(当前代码已实现):
    def ready(self):
        import posts.signals
        from . import tasks
    
  • 确认Celery的autodiscover_tasks()方法正常工作,它会自动扫描所有Django应用下的tasks.py文件。

内容的提问来源于stack exchange,提问作者wicki77

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 11:16:00