简体   繁体   English

Django Celery Beat 在 Django 中使用 Celery 执行定期任务不起作用

[英]Django Celery Beat for periodic task in Django with Celery is not working

I wanted to add celery to my django project.我想将芹菜添加到我的 Django 项目中。 But somehow it does not seem to work as expected although I followed the steps in the official documentation.但不知何故,尽管我按照官方文档中的步骤操作,但它似乎并没有按预期工作。 Here are the parts of my Django project:以下是我的 Django 项目的部分内容:

# celery.py

import os

from celery import Celery
from celery.schedules import crontab

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

app = Celery('bookProjectSetting')

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

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()

app.conf.beat_schedule = {
    'update-lifetime': {
        'task': 'tasks.update_life_time_of_books',
        'schedule': crontab(minute='*/5'),
       
    },
}

Then the init file:然后是init文件:

# __init__.py 
from .celery import app as celery_app

__all__ = ('celery_app',)

Now, in my app folder( called books ), the tasks.py looks like follows:现在,在我的应用程序文件夹(称为books )中, tasks.py如下所示:

from books.models import Book 
from celery import shared_task  

@shared_task
def update_life_time_of_books():
    # Query all the books in our database
    print("Query all books.")
    books = Book.objects.all()

    # do sth. with books. for the sake of brevity I do not show this

In my settings.py file I have this for celery:在我的settings.py文件中,我有这个芹菜:

CELERY_BROKER_URL = 'redis://localhost:6379'
CELERY_TIMEZONE = 'Europe/Berlin'

# ... bla bla

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'django_celery_beat',
    'rest_framework', 
    'books',

]

I started the redis server in a second tab via the redis-server command so that it can take the part as a broker.我通过redis-server命令在第二个选项卡中启动了 redis 服务器,以便它可以充当代理。 I also started my django server via python manage.py runserver .我还通过python manage.py runserver启动了我的 django 服务器。 Finally I started the worker via this command:最后,我通过以下命令启动了工作程序:

celery -A bookProjectSetting beat -l INFO --scheduler django_celery_beat.schedulers:DatabaseScheduler

But as result I got this:但结果我得到了这个:

celery beat v5.0.1 (singularity) is starting.
__    -    ... __   -        _
LocalTime -> 2020-10-23 13:56:29
Configuration ->
    . broker -> redis://localhost:6379//
    . loader -> celery.loaders.app.AppLoader
    . scheduler -> django_celery_beat.schedulers.DatabaseScheduler

    . logfile -> [stderr]@%INFO
    . maxinterval -> 5.00 seconds (5s)
[2020-10-23 13:56:29,666: INFO/MainProcess] beat: Starting...
[2020-10-23 14:00:00,021: INFO/MainProcess] Scheduler: Sending due task update-lifetime (tasks.update_life_time_of_books)

So, it seems that the task is sent after 5 minutes as I declared with the crontab in celery.py (see above) but for example the print("Query all books.") from my task function (see above) is not executed.因此,似乎任务在 5 分钟后发送,因为我在celery.py使用 crontab celery.py (见上文),但例如,我的任务函数(见上文)中的print("Query all books.")没有执行. So, I assume that my task is not executed.所以,我假设我的任务没有执行。 WHY???为什么??? I must say that the official documentation has no clear example and the stuff/articles I have found are only usable with celery 3 or 4. I am using celery 5.我必须说官方文档没有明确的例子,我发现的东西/文章只能与 celery 3 或 4 一起使用。我使用的是 celery 5。

The versions of the libs I use here are these:我在这里使用的库的版本是这些:

  • celery==5.0.1芹菜==5.0.1
  • Django==3.1.2姜戈==3.1.2
  • django-celery-beat==2.1.0 django-celery-beat==2.1.0
  • redis==3.5.3 Redis==3.5.3

My django project folder structure is as follows:我的django项目文件夹结构如下:

bookProject
  |
  |_bookProjectSetting
  |    |_init.py
  |    |_asgi.py
  |    |_celery.py
  |    |_settings.py
  |    |_urls.py
  |    |_wsgi.py
  |
  |_books
  |   |_init.py
  |   |_admin.py
  |   |_apps.py
  |   |_models.py
  |   |_serializers.py
  |   |_tasks.py
  |   |_tests.py
  |   |_urls.py
  |   |_views.py
  |
  |_env
  |_db.sqlite3
  |_dumb.rdb
  |_manage.py

celery -A bookProjectSetting worker -l info and celery -A bookProjectSetting beat -l INFO need to be started at the same time. celery -A bookProjectSetting worker -l infocelery -A bookProjectSetting beat -l INFO需要同时启动。 Then, you can watch your tasks working.然后,您可以观察您的任务的运行情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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