Run Multiple Django Apps With Celery On One Server With Rabbitmq VHosts

29 Apr · by Tim Kamanin · 1 min read

Very often we can have multiple Django apps each running celery and all this is installed on a single server. How do we isolate these separate app's celery workers? The answer is: Simple, if you use Rabbitmq as your broker. Rabbitmq lets us to add multiple virtualhosts, so we can easily separate celery queues and the use different broker_url per our app, let's start: 1) Create rabbitmq server vhost at first:

sudo rabbitmqctl add_vhost our_app_vhost

2) Add user / password pair

sudo rabbitmqctl add_user username password

3) Grant permissions to our user on our vhost:

sudo rabbitmqctl set_permissions -p our_app_vhost username ".*" ".*" ".*"

4) If you want to give our user access to management backend execute this command:

sudo rabbitmqctl set_user_tags username management

5) Now you can add BROKER_URL to your Django app settings:

BROKER_URL = 'amqp://username:password@localhost:5672/our_app_vhost'

Now we use a separate vhost for our Django app. Create as many vhosts as many Django apps you have, so every app could have a "separate" broker to send / receive tasks to / from.

Want to get more 🔥 tips like this one?

Subscribe to get notified about new dev tutorials