Add examples for Celery with Sanic

Hi all,

I have just started using Sanic recently, and it is very handy and matches my use case of a server.

Right now I need to integrate Sanic with Celery workers (running on a different server) but I couldn’t find any clear documentations about how to use Celery with Sanic. The only thing I found is that @ahopkins mentioned in a few topics about his small server running them.

it would be very nice to have an example / template of Sanic with Celery. Or @ahopkins could you please share the template of your project which uses both Sanic and Celery ?

Thank you in advance.

Sure, I can share some code when I’m back in front of a computer.

But it is nothing unique to Sanic. I am using Redis as a broker. I have a common library that is mounted on both my server and my celery worker. The server initiates the task, and the worker picks it up.

More to come later.

1 Like

worker.py

from common.tasks import app

server.py

from common.tasks import do_task

@app.post("/start_task")
async def start_task(request):
    do_task.delay(...)

common/tasks.py

from celery import Celery
from os import environ

REDIS_URI = environ.get("QUEUE_REDIS_URI")
REDIS_PORT = environ.get("QUEUE_REDIS_PORT")
REDIS_DB = environ.get("QUEUE_REDIS_DB")

app = Celery("tasks", broker=f"redis://{REDIS_URI}:{REDIS_PORT}/{REDIS_DB}")

@app.task
def run_task(*args, **kwargs):
    ...
1 Like

Let me know if this helps get you going. The idea is that the Celery app is inside some common library. To run a celery worker, it is enough to just import it inside your worker.py, and then you can run

celery -A worker:app worker --loglevel=info --concurrency=1

Thank you so much for the detailed explanation and the code.

It seems good enough for me to start with.

Is there any advantage of using arq over celery with Sanic?

i’m using arq with sanic. totally ok.