Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running multiple instances of celery on the same server

Tags:

python

celery

I want to run two instances of celery on the same machine. One is for an 'A' version of my application, the other is for the 'B' version.

I have two instances, which I start like this:

(env1)/home/me/firstapp$ celery -A app.tasks worker --config celeryconfig
(env2)/home/me/secondapp$ celery -A app.tasks worker -n Carrot --config celeryconfig

In tasks.py in each application, I create a celery instance like this:

 celery = Celery('tasks', backend='amqp', broker='amqp://[email protected]//')
 @celery.task
 def run_a_task():
     do_stuff()

In env2's task.py, how can I specify that I want to use the second celery instance from secondapp(named Carrot), rather than the first one from firstapp? I suspect I need to change something in the constructor for celery on the first line, but I don't know what to add.

like image 763
munk Avatar asked Apr 24 '13 19:04

munk


People also ask

How do I start multiple Celery workers?

You probably just need to add the --concurrency or -c argument when starting the worker to spawn multiple (parallel) worker instances. Show activity on this post. You can look for Canvas primitives there you can see how to make groups for parallel execution. class celery.

Does Celery use multiprocessing?

Celery itself is using billiard (a multiprocessing fork) to run your tasks in separate processes.

What is Celery instance?

The first thing you need is a Celery instance. We call this the Celery application or just app for short. As this instance is used as the entry-point for everything you want to do in Celery, like creating tasks and managing workers, it must be possible for other modules to import it.

Can a Celery task call another task?

To answer your opening questions: As of version 2.0, Celery provides an easy way to start tasks from other tasks. What you are calling "secondary tasks" are what it calls "subtasks".


1 Answers

I solved this by using a virtual host for celery.

Once the rabbitmq server is running I issue these commands:

rabbitmqctl add_user user password
rabbitmqctl add_vhost app2
rabbitmqctl set_permissions -p app2 user ".*" ".*" ".*"

Then I start celery with:

celery -A tasks worker --broker=amqp://user:password@localhost/app2

With my task, I initialize the celery object like this:

celery = Celery('tasks', backend='amqp', broker='amqp://user:password@localhost:5672/app2
like image 64
munk Avatar answered Nov 15 '22 19:11

munk