Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i use celery with different code base in API and workers

Currenty I have one Ec2 Instance for incomming API requests

  1. User orders goes to queue in redis via celery task
  2. I have 2 Ec2 instances processing queues

The problem is the code base is same on both API and celery workers. because e,g

I use cutsomer.process_order(order_id)

Then worker does the rest.

Is there any way that i separate API from worker code. I want to make API in separate code base and workers in separate code base

like image 372
Karl Avatar asked May 02 '16 03:05

Karl


People also ask

What is broker and backend in Celery?

Celery has the ability to communicate and store with many different backends (Result Stores) and brokers (Message Transports).

Can I use Celery without Django?

Yes you can. Celery is a generic asynchronous task queue.

How do you call Celery synchronously?

If you look at the celery DOCS on tasks you see that to call a task synchronosuly, you use the apply() method as opposed to the apply_async() method. The DOCS also note that: If the CELERY_ALWAYS_EAGER setting is set, it will be replaced by a local apply() call instead.


1 Answers

Your API code can call any Celery task without having the task source code available. Celery has a feature called signatures:

from celery import Celery
app = Celery(...)

process_order = app.signature('your-other-project.tasks.process_order')
result = process_order.delay(order_id)  # standard calling api works
print(result.get())

Just make sure both your API and worker connect to the same message broker and your worker actually has the task named in the signature.

like image 62
tuomur Avatar answered Oct 08 '22 18:10

tuomur