在python flask中未使用芹菜执行多个任务
我有两个文件。logic.py和app.py在逻辑文件中有各种函数,app文件是一个flask文件。在app.py文件中,我写了一些东西来设置芹菜的工作方式,以便它在特定的时间间隔安排任务。 my logic.py与此类似:在python flask中未使用芹菜执行多个任务,python,flask,redis,celery,celery-task,Python,Flask,Redis,Celery,Celery Task,我有两个文件。logic.py和app.py在逻辑文件中有各种函数,app文件是一个flask文件。在app.py文件中,我写了一些东西来设置芹菜的工作方式,以便它在特定的时间间隔安排任务。 my logic.py与此类似: #import statements class xyz: @celery.task(name='scheduler',once={'graceful':True}) def scheduler(self) #rest of the code
#import statements
class xyz:
@celery.task(name='scheduler',once={'graceful':True})
def scheduler(self)
#rest of the code
@celery.task(name='scheduler2',once={'graceful':True})
def scheduler2(self):
#rest of the code
还有我的app.py
from flask import Flask
from celery import Celery
from celery.schedules import crontab
from celery.utils.log import get_task_logger
app = Flask(__name__)
logger = get_task_logger(__name__)
def make_celery(app):
#Celery configuration
app.config['CELERY_BROKER_URL'] = 'redis://localhost:6379/0'
app.config['CELERY_RESULT_BACKEND'] = 'redis://localhost:6379/0'
app.config['CELERY_TASK_SERIALIZER'] = 'json'
app.config['CELERY_RESULT_SERIALIZER'] = 'json'
app.config['CELERY_ACCEPT_CONTENT'] = ['json','yaml','msgpack']
app.config['CELERY_TASK_RESULT_EXPIRES'] = 30
app.config['CELERY_TIMEZONE'] = 'Asia/Calcutta'
app.config['CELERY_TASK_ACKS_LATE'] = True
app.config['CELERY_WORKER_PREFETCH_MULTIPLIER'] = 1
app.config['CELERYBEAT_SCHEDULE'] = {
# Executes every 5 minutes
'scheduler-every-minute': {
'task': 'scheduler',
'schedule': crontab('*/5'),
'args': ({ },)
},
'scheduler2-every-thursday': {
'task': 'scheduler2',
'schedule': crontab(day_of_week='Thursday',hour=12,minute=40),
'args': ({ }, )
},
}
celery = Celery(app.import_name, broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)
TaskBase = celery.Task
class ContextTask(TaskBase):
abstract = True
def __call__(self, *args, **kwargs):
with app.app_context():
return TaskBase.__call__(self, *args, **kwargs)
celery.Task = ContextTask
return celery
celery = make_celery(app)
我更关心的是,为什么在运行celerybeat和worker时,它只执行一个任务。我想在我的机器上并行运行这两个任务,但是只执行第一个任务。有人能帮我找出哪里出了问题吗?你的第二个任务可能是在周四执行。您可以测试第二个任务,可能每6分钟运行一次,以查看两个任务是否可以并行运行。@DDhaliwal我尝试过,但只有第一个任务运行,当我尝试关闭它时,会发生热关机(关闭工作进程需要很长时间,因为它确保结束任务,然后关闭工作进程),因此我再次尝试关闭它,这一次是冷停堆