Question

I'm referring to Django Celery documents.

I created celery.py in my proj/proj just as the document said. and then included __init__.py

celery.py

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

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')
app = Celery('proj')
app.conf.update(
    CELERY_RESULT_BACKEND='djcelery.backends.database:DatabaseBackend',
)
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

__init__.py

from __future__ import absolute_import
from .celery import app as celery_app

I installed pip install django-celery , then migrated python manage.py migrate djcelery It made some of the tables in my DB.

tasks.py

from __future__ import absolute_import
from celery import shared_task
import requests
import json

@shared_task
def post_notification(data,url):
    headers = {'content-type': 'application/json'}
    requests.post(url, data=json.dumps(data), headers=headers)

After that I called my task in my views as

task = post_notification.delay(data,url)
print task.id #it prints an id
print task.status # prints PENDING

But nothing gets logged into any of my tables.

I've read my threads on SO,Thread1 , Thread2 and many more given on these threads, but nothing happens.

It provides me the ID & status of the task but how do I save the task in the DB? Usually it should get logged into celery_taskmeta, but there's nothing in there.

Though the task gets execute but I want to save the task in DB as well. How can I do it? Is there something I'm missing?

Was it helpful?

Solution 2

Add following in settings.py file

BROKER_URL = 'amqp://guest:guest@localhost//'
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'

And start the worker.

OTHER TIPS

try this in celery.py

from __future__ import absolute_import
import os
from celery import Celery
from django.conf import settings
from celery.schedules import crontab

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app_name.dev_settings')

app = Celery('app_name')

# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
app.conf.CELERY_TIMEZONE = 'UTC'
app.conf.update(
    CELERY_RESULT_BACKEND='djcelery.backends.database:DatabaseBackend',
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top