Question

I'm trying to set up two tasks that both run every minute. Is there any way to group them in one to run?

I specified CELERYBEAT_SCHEDULE in my celeryconfig.py as following:

CELERYBEAT_SCHEDULE = {
    'every-minute': {
        'task': 'tasks.add',
        'schedule': crontab(minute='*/1'),
        'args': (1,2)
    },
}

So if I want to run two tasks, I would expect something like this?

CELERYBEAT_SCHEDULE = {
    'every-minute': {
        'task': ['tasks.add','task.multiply'],
        'schedule': [crontab(minute='*/1'),crontab(minute='*/1')],
        'args': [(1,2),(3,4)]
    },
}

However it did not work. Is there any standard way of doing this?

Était-ce utile?

La solution

The Celery Documentation: Periodic Tasks states that you can only have the name of the task to be executed (not a list, etc.)

You could create two different schedule entries:

CELERYBEAT_SCHEDULE = {
    'every-minute_add': {
        'task': 'tasks.add',
        'schedule': crontab(minute='*/1'),
        'args': (1,2)
    },
    'every-minute_multiply': {
        'task': 'task.multiply',
        'schedule': crontab(minute='*/1'),
        'args': (3,4)
    },
}

Autres conseils

CELERYBEAT_SCHEDULE = {
'every-minute': {
    'task': 'tasks.add',
    'schedule': crontab(minute='*/1'),
    'args': (1,2)
},

}

You can use single task celery for your work to be done as per your question. Your tasks.py should be

def multiply(p1,p2):
  return p1*p2

def add(x,y):
  z=multiply(3,4)
  return x+y,z
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top