2

When i'm trying to run celery background task then it giving me error: KeyError: 'myproject.tasks.async_task'

I'm using python version: 3.8.10, django verion: 4.1.2 , celery version: 5.2.7 and rabbitmq version: 3.8.2

here below screenshot is my project structure:

enter image description here

here below is my code for background task:

settings.py:

CELERY_BROKER_URL = 'amqp://localhost:5672'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_SERIALIZER = 'json'

myproject/init.py:

from __future__ import absolute_import, unicode_literals
from myproject.celery import app as celery_app

__all__ = ['celery_app']

celery.py

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')

app = Celery('myproject')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()

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

tasks.py:

from __future__ import absolute_import
from celery import shared_task
from time import sleep
import os

@shared_task
def sleepy(duration):
    sleep(duration)
    return None
 
@shared_task
def async_task(save_path, name_of_file):
    sleep(30)
    completeName = os.path.join(save_path, name_of_file+".html")         
    file1 = open(completeName, "w")
    toFile = 'test data'
    file1.write(toFile)
    file1.close()
    return 'task complete'

views.py


def add_product(request):
    if request.method == 'POST':
        id_col_data = request.POST.getlist('services')
        target_col = request.POST.get('target_col')
        file_name = request.POST.get('file_name')
        amz_columns_dict = {'id_col': id_col_data,
                            'target_col': target_col,
                            'wt_col': None}
        import os.path
        save_path = '/home/satyajit/Desktop/'
        if os.path.exists(save_path):
            try:
                from myproject.tasks import async_task
                name_of_file = file_name
                status = async_task.delay(save_path, name_of_file)   #celery task
                print('status---->', status)
            except Exception as e:
                print('task error is ------>', e)
                return render(request,'data/error.html', {'message': 'async task error'})
        else:
            print('error occured')
        return HttpResponse('product added successfully')
    return render(request, 'add_product.html')

When i run this celery command celery -A myproject worker -l info to check the celery process work then i get below trackback. The traceback full details on below screenshot:

enter image description here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.