How to restart Celery on file change

Tue 07 April 2020, by Matthew Segal
Category: Django

I use Celery and Django together a lot. My biggest pain when doing local development with Celery is that the worker process won't restart when I change my code. Django's runserver restarts on code change, why can't Celery? How can you set up your dev envrionent to force Celery to restart on file change?

Watchdog is a nifty little (cross-platform?) Python library that watches for filesystem changes. We can use Watchdog to restart anything on file change.

First, you need to install it in your local Python environment:

pip install watchdog[watchmedo]

Let's say you normally start Celery like this:

celery worker --broker redis://localhost:6379 --app myapp

Now you can start it like this:

watchmedo \
    auto-restart \
    --directory ./my-code/ \
    --recursive \
    --pattern '*.py' \
    -- \
    celery worker --broker redis://localhost:6379 --app myapp

That's it! Watchdog will restart the process on file change. If you like, you can specify:

  • multiple code directories using --directory
  • different file patterns using --pattern

If you have any feedback or questions email me at [email protected]