Django Celery 任务不会在开发中触发
Django Celery task doesn't fire in development
我正在尝试使用周期性任务,但无法正常工作。
我有这个测试任务
# handler/tasks.py
from celery import Celery
app = Celery()
@app.on_after_configure.connect
def setup_periodic_tasks(sender, **kwargs):
# Calls test('hello') every 2 seconds.
sender.add_periodic_task(2, test.s('hello'), name='add every 2')
@app.task
def test(arg):
print(arg)
芹菜已配置
# project dir
# salaryx_django/celery.py
from __future__ import absolute_import
import os
from celery import Celery
from django.conf import settings
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'salaryx_django.settings')
app = Celery('salaryx_django')
# 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.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
# salaryx_django/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_TIMEZONE = 'Europe/London'
工人们开始行动
[2022-04-25 14:57:55,424: INFO/MainProcess] Connected to redis://localhost:6379//
[2022-04-25 14:57:55,426: INFO/MainProcess] mingle: searching for neighbors
[2022-04-25 14:57:56,433: INFO/MainProcess] mingle: all alone
[2022-04-25 14:57:56,452: WARNING/MainProcess] /Users/jonas/Desktop/salaryx_django/venv/lib/python3.8/site-packages/celery/fixups/django.py:203: UserWarning: Using settings.DEBUG leads to a memory
leak, never use this setting in production environments!
warnings.warn('''Using settings.DEBUG leads to a memory
[2022-04-25 14:57:56,453: INFO/MainProcess] celery@Air-von-Jonas ready.
Redis 正在等待连接
但什么也没发生..
芹菜节拍
(venv) jonas@Air-von-Jonas salaryx_django % celery -A salaryx_django beat
celery beat v5.2.6 (dawn-chorus) is starting.
__ - ... __ - _
LocalTime -> 2022-04-26 05:38:27
Configuration ->
. broker -> redis://localhost:6379//
. loader -> celery.loaders.app.AppLoader
. scheduler -> celery.beat.PersistentScheduler
. db -> celerybeat-schedule
. logfile -> [stderr]@%WARNING
. maxinterval -> 5.00 minutes (300s)
你还要运行打败。
来自beat entries 文档
The add_periodic_task() function will add the entry to the beat_schedule setting behind the scenes
只需 运行宁 celery -A salaryx_django beat
在另一个过程中应该让你去。阅读文档以获取更多信息。
我正在尝试使用周期性任务,但无法正常工作。
我有这个测试任务
# handler/tasks.py
from celery import Celery
app = Celery()
@app.on_after_configure.connect
def setup_periodic_tasks(sender, **kwargs):
# Calls test('hello') every 2 seconds.
sender.add_periodic_task(2, test.s('hello'), name='add every 2')
@app.task
def test(arg):
print(arg)
芹菜已配置
# project dir
# salaryx_django/celery.py
from __future__ import absolute_import
import os
from celery import Celery
from django.conf import settings
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'salaryx_django.settings')
app = Celery('salaryx_django')
# 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.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
# salaryx_django/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_TIMEZONE = 'Europe/London'
工人们开始行动
[2022-04-25 14:57:55,424: INFO/MainProcess] Connected to redis://localhost:6379//
[2022-04-25 14:57:55,426: INFO/MainProcess] mingle: searching for neighbors
[2022-04-25 14:57:56,433: INFO/MainProcess] mingle: all alone
[2022-04-25 14:57:56,452: WARNING/MainProcess] /Users/jonas/Desktop/salaryx_django/venv/lib/python3.8/site-packages/celery/fixups/django.py:203: UserWarning: Using settings.DEBUG leads to a memory
leak, never use this setting in production environments!
warnings.warn('''Using settings.DEBUG leads to a memory
[2022-04-25 14:57:56,453: INFO/MainProcess] celery@Air-von-Jonas ready.
Redis 正在等待连接
但什么也没发生..
芹菜节拍
(venv) jonas@Air-von-Jonas salaryx_django % celery -A salaryx_django beat
celery beat v5.2.6 (dawn-chorus) is starting.
__ - ... __ - _
LocalTime -> 2022-04-26 05:38:27
Configuration ->
. broker -> redis://localhost:6379//
. loader -> celery.loaders.app.AppLoader
. scheduler -> celery.beat.PersistentScheduler
. db -> celerybeat-schedule
. logfile -> [stderr]@%WARNING
. maxinterval -> 5.00 minutes (300s)
你还要运行打败。
来自beat entries 文档
The add_periodic_task() function will add the entry to the beat_schedule setting behind the scenes
只需 运行宁 celery -A salaryx_django beat
在另一个过程中应该让你去。阅读文档以获取更多信息。