M
M
Maxim2016-11-09 23:31:13
Django
Maxim, 2016-11-09 23:31:13

How to know if celery is executing a function asynchronously?

I am attaching celery settings

# settings.py

# CELERY STUFF
BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'

# celery.py

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

#
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'backend.settings')
app = Celery('backend')

#
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)


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

# tasks.py

import json
import requests
from celery.decorators import task
from celery.task.schedules import crontab
from celery.decorators import periodic_task
from celery.utils.log import get_task_logger
from api.controllers.message import message_delayed


logger = get_task_logger(__name__)

@periodic_task(
    run_every=(crontab(minute='*/1')),
    ignore_result=True
)
def period():
    # req = requests.get('')
    req = message_delayed()
    send(req)
    logger.info('task success')

@task
def send(mes):
    print('it works')
    # do_something

the structure
8929026856fe4fa497ac6ef4ef5bd839.png
in view.py I wrap the function in a task, I added print() output to see if the worker works or not, as a result, nothing is executed, the method twitches on the server, everything works fine in task.py
When the worker starts, it displays the information that it finds this task
[tasks]
  . api.tasks.period
  . api.tasks.send
  . api.views.user_get
  . backend.celery.debug_task

Everything should work according to the idea, but in the end I don’t know if it works, who can tell me what?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mykola, 2016-11-10
@IKMOL

I don’t know what other settings you have there and what tasks but try this:
CELERY_RESULT_BACKEND='djcelery.backends.database:DatabaseBackend',
BROKER_URL='django://'
but in production you need to (correctly) use another broker, for example: RabbitMQ, Redis ...

X
xozzslip, 2016-11-10
@xozzslip

sudo apt install rabbitmq-server

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question