gpt4 book ai didi

python - 如何使用 celery-beat 启动任务?

转载 作者:行者123 更新时间:2023-12-03 06:35:55 25 4
gpt4 key购买 nike

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

项目/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)

项目/__init.py__
from .celery import app as celery_app

__all__ = ['celery_app']

celery -A proj worker -l 信息
[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 -A proj beat -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())

最佳答案

celery 节拍命令

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

该命令用于启动 celery beat。

首先添加 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)

项目/__init.py__
from .celery import app as celery_app

**__all__** = ['celery_app']

关于python - 如何使用 celery-beat 启动任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58658363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com