简体   繁体   中英

Celery task will not execute

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?

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" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM