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

Celery执行后台任务时触发KeyError: 'myproject.tasks.async_task'错误求助

Fixing KeyError: 'myproject.tasks.async_task' in Celery + Django Setup

Hey there, let's work through that frustrating KeyError you're hitting when running your Celery tasks. I've gone over your code and configuration, and here are the most actionable fixes to get things working smoothly:

1. Fix Task Auto-Discovery

Celery's app.autodiscover_tasks() defaults to looking for tasks.py files inside Django apps listed in INSTALLED_APPS, not your project root directory. Since your tasks.py lives directly in myproject/, you need to explicitly tell Celery to scan that directory:

Update your celery.py file with these tweaks:

from __future__ import absolute_import
import os, sys
from celery import Celery
import django
from django.conf import settings

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
django.setup()  # Add this to initialize Django's environment properly
app = Celery('myproject')
app.config_from_object('django.conf:settings', namespace='CELERY')
# Explicitly point Celery to your project root for tasks.py
app.autodiscover_tasks(['myproject'])

@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

Alternatively, you could move your tasks.py into one of your Django apps (like the data app referenced in your views) and ensure that app is listed in INSTALLED_APPS in settings.py—this is the more standard approach for Django + Celery setups.

2. Verify Worker Launch Directory

Make sure you're running the Celery worker command from your project root directory (the same folder where manage.py lives):

celery -A myproject worker -l info

If you run this from a subdirectory, Celery won't be able to resolve the myproject.tasks module correctly.

3. Clear Stale Task Caches

Old task metadata can sometimes get stuck in RabbitMQ or Celery's internal cache. Let's reset things:

  • Stop your Celery worker first
  • Restart RabbitMQ to clear queued tasks:
    sudo service rabbitmq-server restart
    
  • Restart your Celery worker with the updated configuration

4. Check Task Registration Logs

When you start the Celery worker, look for log lines like this:

[202X-XX-XX XX:XX:XX,XXX: INFO/MainProcess] Connected to amqp://guest:**@localhost:5672//
[202X-XX-XX XX:XX:XX,XXX: INFO/MainProcess] celery@yourhostname ready.
[202X-XX-XX XX:XX:XX,XXX: INFO/MainProcess] Loaded tasks from myproject.tasks

If you don't see that last line about loading tasks from myproject.tasks, double-check your autodiscover_tasks configuration and make sure tasks.py is in the correct location.

5. Double-Check Import Paths

In your views.py, you're using the correct absolute import with from myproject.tasks import async_task—stick with this, as relative imports can cause module resolution issues in Celery.

After trying these steps, your async_task should be properly registered, and the KeyError should disappear when you trigger the task from your view.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 13:38:13