Rohitashv Singhal
Rohitashv Singhal

Reputation: 4557

Practical examples on redis and celery

I am new to redis and celery. I have gone through the basic tutorial of both, but I am not getting how to implement then in task scheduling job

I am unable to start with the scripting part. I am not getting how to write a script to make a queue, run the workers etc. I would need a practical example

Upvotes: 3

Views: 6567

Answers (1)

Rostyslav Dzinko
Rostyslav Dzinko

Reputation: 40755

So here's a cannonical example of how can celery run with Redis (let the script filename be mytasks.py):

from celery import Celery

celery = Celery('tasks', broker='redis://localhost:6379/0')

@celery.task
def add(x, y):
    return x + y

As you see, broker argument was set to use Redis installed on your local machine. The next thing is to start celery server:

$ celery -A mytasks worker --loglevel=info

As your tasks celery server has been started, you can now use it to run your task just by importing mytasks script, e.g from Python interpreter interactive mode:

>>> from mytasks import add
>>> add.delay(1, 1)
2

After some time '2' will appear in console.

That's a basic example of how you can setup your tasks execution environment.

Upvotes: 10

Related Questions