6

I've followed the instruction process to installing and setting up celery, now I'm trying to execute my task. My project tree looks like this:

bin
draft1--
        |
         -------draft1 ----
                           |
                            --------celery.py
                            --------tasks.py
                            --------views.py
         -------manage.py
         -------templates

include
lib

Here's my code:

settings.py

CELERY_BROKER_URL = 'amqp://guest:guest@localhost:5672//' 

celery.py

import os
from celery import Celery

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings')

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

tasks.py

from celery import shared_task

@shared_task
def print_this():
    print('ONE MINUTE')

app.views

print_this.delay()

So my celery function doesn't work, it doesn't execute the print statement. What I want to do is execute the function every minute. Any idea what the problem is?

1 Answer 1

2

I think you need to read more before just starting to experiment. Celery is a distributed task queue, which basically means, it polls a queue to see if there is any task that needs to be run. If there is, it runs the task.

About your setup, you seem to have a task runner, but not the queue that runner requires to poll to check if there is any tasks to be run. The configuration CELERY_BROKER_URL is about that queue. I suggest you start by reading "Celery's Introduction documents". Especially "What do I need?" part.

NOTE FOR AFTER YOU FIGURE OUT THE QUEUE PART

Also, I am not sure how do you run and serve your django application, but celery requires separate processes. For that part you will need to skim "First Steps with Celery".

Sign up to request clarification or add additional context in comments.

7 Comments

I've read everything and I'm not sure there's anything to "figure out" about the queue part? I'm running my app in django's local server so the CELERY_BROKER_URL should be the same for me as in the instructions. I tried./bin/celery -A draft1 worker -l info in my project root (where manage.py is) and it returned this error: AttributeError: module 'draft1' has no attribute 'app' / During handling of the above exception, another exception occurred: AttributeError: module 'draft1' has no attribute 'celery' Any idea what this could mean? (draft1 is my app name)
Did you setup a queue server? Like rabbitmq?
Yes, both my django server and rabbitmq are running.
Then add a user to rabbitmq. And point your django + celery application to your rabbitmq broker with CELERY_BROKER_URL = 'amqp://user:password@host:port' (assuming you are running rabbitmq server on localhost)
I've changed my CELERY_BROKER_URL to the correct value, and I'm logged in to my RabbotMQ in my browser. Can you tell me how I can execute my function? I believe I have all the tools in place however my function does not work. All my code is in my post.
|

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.