简体   繁体   English

如何使用 Celery 获取计划任务列表及其 arguments?

[英]How to get list of scheduled tasks and their arguments with Celery?

I'd like to inspect prior scheduled tasks before creating a new one to prevent duplicates.我想在创建新任务之前检查之前的计划任务以防止重复。 How can I get a list of all scheduled tasks and their arguments using python and celery?如何使用 python 和 celery 获取所有计划任务及其 arguments 的列表?

my_proj/celery.py my_proj/celery.py

from celery import Celery
from celery.schedules import crontab

app = Celery("my_proj")

app.conf.update(
    imports=["task"],
    timezone="UTC",
    beat_schedule={
        "task.common.add": {
            "task": "task.common.add",
            "schedule": crontab(),
            'args': (1, 2),
        },
        "task.common.mul": {
            "task": "task.common.mul",
            "schedule": crontab(minute=0, hour=0),
            'args': (3,),
            'kwargs': {'y': 4},
        },
    },
)

task/common.py任务/common.py

from my_proj.celery import app  # Use @shared_task if in Django

@app.task
def add(x, y):
    print(f"{x}, {y}, {x + y}")
    return x + y


@app.task
def mul(x, y):
    print(f"{x}, {y}, {x * y}")
    return x * y

Option 1:选项1:

>>> from my_proj.celery import app
>>> app.conf.beat_schedule
{'task.common.add': {'task': 'task.common.add', 'schedule': <crontab: * * * * * (m/h/d/dM/MY)>, 'args': (1, 2)}, 'task.common.mul': {'task': 'task.common.mul', 'schedule': <crontab: 0 0 * * * (m/h/d/dM/MY)>, 'args': (3,), 'kwargs': {'y': 4}}}

Option 2:选项 2:

If using a file scheduler eg celery.beat.PersistentScheduler (default) which writes to a local shelve database file of name celerybeat-schedule .如果使用文件调度程序,例如celery.beat.PersistentScheduler (默认),它将写入名为celerybeat-schedule的本地搁置数据库文件。

  • Warning: This might not work if the scheduler is currently running because the file would be locked and cannot be read.警告:如果调度程序当前正在运行,这可能不起作用,因为文件将被锁定且无法读取。
>>> import shelve
>>> with shelve.open('celerybeat-schedule') as schedule:
...     print(schedule['entries'])
... 
{'task.common.mul': <ScheduleEntry: task.common.mul task.common.mul(3, y=4) <crontab: 0 0 * * * (m/h/d/dM/MY)>, 'task.common.add': <ScheduleEntry: task.common.add task.common.add(1, 2) <crontab: * * * * * (m/h/d/dM/MY)>, 'celery.backend_cleanup': <ScheduleEntry: celery.backend_cleanup celery.backend_cleanup() <crontab: 0 4 * * * (m/h/d/dM/MY)>}

Option 3:选项 3:

If using a database scheduler eg django-celery-beat or celery-sqlalchemy-scheduler , just query the database records.如果使用数据库调度程序,例如django-celery-beatcelery-sqlalchemy-scheduler ,只需查询数据库记录。 So if using django-celery-beat, it would be:因此,如果使用 django-celery-beat,它将是:

>>> from django_celery_beat.models import PeriodicTask, PeriodicTasks
>>> PeriodicTask.objects.all()
<ExtendedQuerySet [<PeriodicTask: celery.backend_cleanup: 0 4 * * * (m/h/dM/MY/d) UTC>, <PeriodicTask: task.common.add: * * * * * (m/h/dM/MY/d) UTC>, <PeriodicTask: task.common.mul: 0 0 * * * (m/h/dM/MY/d) UTC>]>
>>> PeriodicTask.objects.values('name', 'task', 'args', 'kwargs')
<ExtendedQuerySet [{'name': 'celery.backend_cleanup', 'task': 'celery.backend_cleanup', 'args': '[]', 'kwargs': '{}'}, {'name': 'task.common.add', 'task': 'task.common.add', 'args': '[1, 2]', 'kwargs': '{}'}, {'name': 'task.common.mul', 'task': 'task.common.mul', 'args': '[3]', 'kwargs': '{"y": 4}'}]>

I am putting this here for my own record, mainly, for the next time I need it.我把它放在这里是为了我自己的记录,主要是为了下次我需要它的时候。 The syntax that worked for me:对我有用的语法:

from celery import app
app.app_or_default().conf.beat_schedule

gets得到

{'batch1': {'schedule': 900,
  'task': 'app.tasks.batch1',
  'args': (),
  'kwargs': {},
  'options': {}},
 'batch2': {'schedule': 600,
  'task': 'app.tasks.batch2',
  'args': (),
  'kwargs': {},
  'options': {}},
  ....

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

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