Django celery: how to set up a task to run at a specific interval programmatically

I found that I can set up a task for a certain period of time at a certain time here , but this was done only during the announcement of the task. How to set up a dynamic task?

+5
source share
4 answers

The graph is derived from a parameter , and thus seems unchanged at runtime.

Perhaps you can accomplish what you are looking for using Task ETAs . This ensures that your task will not be completed until the desired time, but does not promise to start the task at the appointed time - if workers are overloaded in the designated ETA, the task can work later.

If this restriction is not a problem, you can write a task that first starts as:

@task
def mytask():
    keep_running = # Boolean, should the task keep running?
    if keep_running:
        run_again = # calculate when to run again
        mytask.apply_async(eta=run_again)
    # ... do the stuff you came here to do ...

The main disadvantage of this approach is that you rely on the store to remember about the tasks in flight. If one of them fails and then turns off, then the task will never start again. If your broker is not saved to disk and he dies (taking all the tasks in flight with him), then none of these tasks will start again.

- "", , .

, , , .

+7

celery.task.base.PeriodicTask is_due, , . , . . : http://docs.celeryproject.org/en/latest/reference/celery.task.base.html?highlight=is_due#celery.task.base.PeriodicTask.is_due

:

import random
from celery.task import PeriodicTask

class MyTask(PeriodicTask):

    def run(self, **kwargs):
        logger = self.get_logger(**kwargs)
        logger.info("Running my task")

    def is_due(self, last_run_at):
        # Add your logic for when to run. Mine is random
        if random.random() < 0.5:
            # Run now and ask again in a minute
            return (True, 60)
        else:
            # Don't run now but run in 10 secs
            return (True, 10)
+1
0

... http://celery.readthedocs.org/en/latest/faq.html#can-i-change-the-interval-of-a-periodic-task-at-runtime

, , , , .

CELERYBEAT_SCHEDULE = {    
    "my_name": {
        "task": "myapp.tasks.task",
        "schedule": myschedule(),    
    }
}

CELERYBEAT_MAX_LOOP_INTERVAL, , , .

0
source

All Articles