7

I've recently been playing around with django and celery. One annoying thing during development is the fact that I have to restart the celery daemon each time I modify a task. When I'm developing, I usually like to use 'manage.py runserver' which automatically reloads the django framework on modifications to my apps.

Is there a way to add a hook to the reloading process that runserver does so that it automatically restarts the celery daemon I have running?

Alternatively, does celery have a similar monitor-and-reload-on-change mode that I should be using for development?

GrantJ
  • 8,162
  • 3
  • 52
  • 46

5 Answers5

4

Django-supervisor works very well for this purpose. You can have it start the Django server, Celery, and anything else you need, and have different configurations for development and production servers. It also knows to reload the celery daemon when your code changes. https://github.com/rfk/django-supervisor

LS55321
  • 883
  • 1
  • 12
  • 21
2

Yes. Django provides auto reload hook, which can be used to restart other scripts.

Here is a simple management command which prints a message on reload

import subprocess

from django.core.management.base import BaseCommand
from django.utils import autoreload


def reload():
    print('Code changed. Auto reloading...')    

class Command(BaseCommand):

    def handle(self, *args, **options):
        autoreload.main(reload)

Now you can save to a reload.py and run it with python manage.py reload. A management command to reload celery workers is available here.

Chillar Anand
  • 27,936
  • 9
  • 119
  • 136
2

I believe you can set CELERY_ALWAYS_EAGER to true.

goh
  • 27,631
  • 28
  • 89
  • 151
1

Celery 2.5 has an experimental runtime option --autoreload that could be used for this purpose, too. Here's more detail in the release notes. That being said, I think django-supervisor (via @Lee Semel) looks like the better way of doing things. I thought I would post this alternative here in case other readers do not want to have to configure another app for asynchronous processing.

dino
  • 3,093
  • 4
  • 31
  • 50
1

Celery didn't have any feature for reload code or for auto restart when the code change, than you have to restart it manually.

There isn't a way for add an hook, and I think not worthwhile of edit the source code of django just for perform a restart. Personally while I'm developing i prefere to see the output shell of celery that is decorated with color instead of tail the logs, is more readable.

Mauro Rocco
  • 4,980
  • 1
  • 26
  • 40