Currenty I have one Ec2 Instance for incomming API requests
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
Celery has the ability to communicate and store with many different backends (Result Stores) and brokers (Message Transports).
Yes you can. Celery is a generic asynchronous task queue.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With