Search code examples
pythondjangocelerydjango-celerycelerybeat

How do you create a singleton celery task that runs indefinitely upon starting a Django application?


Right now I have a @sharedtask that follows the following code exactly: http://docs.celeryproject.org/en/latest/tutorials/task-cookbook.html#id1

And the following scheduler settings:

CELERYBEAT_SCHEDULE = {
    'indefinite-task': {
        'task': 'app.tasks.indefinite_task',
        'schedule': timedelta(seconds=1),
    },
}

But my two of my workers seem to be running the task more than once, presumably because of a worker having concurrent threads? How can I ensure that the task is only run once by a single thread on a single worker?

Example of what I am seeing in the shell:

11:00:43 worker.1 | [2015-05-05 15:00:43,444: DEBUG/Worker-1] app.tasks.indefinite_task[b8b0445c-91e6-4652-bd75-4c609149161f]: Acquiring lock
11:00:43 worker.1 | [2015-05-05 15:00:43,444: DEBUG/Worker-2] app.tasks.indefinite_task[b8b0445c-91e6-4652-bd75-4c609149161f]: Acquiring lock

Edit: I'm using a Procfile with heroku.


Solution

  • Perhaps you want to run the python script in it's own worker process.

    Ex:

    worker: python indefinite_task.py

    This ensures that only one task is run for that worker.