redis 如何使用celery beat运行周期性任务?

bvjxkvbb  于 2023-10-15  发布在  Redis
关注(0)|答案(2)|浏览(101)

为什么我不能运行定期任务?

项目/settings.py

REDIS_HOST = 'localhost'
REDIS_PORT = '6379'
CELERY_BROKER_URL = 'redis://localhost:6379'
BROKER_URL = 'redis://' + REDIS_HOST + ':' + REDIS_PORT

CELERY_BEAT_SCHEDULE = {
    'task-first': {
        'task': 'app.tasks.one',
        'schedule': timedelta(seconds=1)
    },
    'task-second': {
        'task': 'app.tasks.two',
        'schedule': crontab(minute=0, hour='*/3,10-19')
    }
}

项目/celery.py

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

app = Celery('proj')
app.config_from_object('django.conf:settings')

app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

proj/init.py

from .celery import app as celery_app

__all__ = ['celery_app']

celery -A proj worker -l info

[2019-10-31 16:57:57,906: INFO/MainProcess] Connected to redis://localhost:6379//
[2019-10-31 16:57:57,912: INFO/MainProcess] mingle: searching for neighbors
[2019-10-31 16:57:58,927: INFO/MainProcess] mingle: all alone
[2019-10-31 16:57:58,951: INFO/MainProcess] celery@lexvel-MS-7A72 ready.
  • 任务已找到 *
    celery -一个项目节拍-l信息
Configuration ->
    . broker -> redis://localhost:6379//
    . loader -> celery.loaders.app.AppLoader
    . scheduler -> celery.beat.PersistentScheduler
    . db -> celerybeat-schedule
    . logfile -> [stderr]@%INFO
    . maxinterval -> 5.00 minutes (300s)
[2019-10-31 16:58:02,851: INFO/MainProcess] beat: Starting...

创建celerybeat-shedule文件。但除了这几行之外,再没有别的显示了。

任务

@task()
def one():
    print('start 1', datetime.now())
    driver = init_driver()
    parse(driver)
    driver.close()
    driver.quit()
​
​
@task()
def two():
    print('start 2', datetime.now())
    driver = init_driver()
    parse2(driver)
    driver.close()
    driver.quit()
    print('end 2', datetime.now())
de90aj5v

de90aj5v1#

celery 节拍命令

celery -A proj worker -l info -B --scheduler django_celery_beat.schedulers:DatabaseScheduler

此命令用于启动celery 节拍。
首先在设置文件中的已安装应用程序中添加django_celery_beat模块。
然后应用django migrate命令,这将在管理面板中创建表。
完成所有过程后,如celery 文件和创建任务在tasks.py
你将应用上面提到的beat命令。

项目/settings.py

INSTALLED_APPS = [

    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'django_celery_beat',
]

REDIS_URL = "redis://localhost:6379/1"

CELERY_BROKER_URL=REDIS_URL

CELERY_RESULT_BACKEND=REDIS_URL

CELERY_ACCEPT_CONTENT = ['application/json']

CELERY_RESULT_SERIALIZER = 'json'

CELERY_TASK_SERIALIZER = 'json'

CELERY_BEAT_SCHEDULE = {

        'task-first': {
        'task': 'app.tasks.one',
        'schedule': timedelta(seconds=1)
       },
      'task-second': {
        'task': 'app.tasks.two',
        'schedule': crontab(minute=0, hour='*/3,10-19')
      }
}

项目/celery.py

from celery import Celery

from django.conf import settings

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

app = Celery('proj')

app.config_from_object('django.conf:settings')

app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

proj/init.py

from .celery import app as celery_app

**__all__** = ['celery_app']
o4tp2gmn

o4tp2gmn2#

当你运行你的worker时,它是否说app.tasks.oneapp.tasks.two是注册的任务?如果它们没有作为注册任务出现在那里,那么您的节拍正在调度无法执行的任务。- 它们只会在队列中等待,最终过期。另一种检查它们是否已注册的方法是通过celery -A proj.celeryapp inspect registered(将proj.celeryapp更改为Celery应用程序的位置)。

相关问题