Workflow
Install message broker
For example, rabbitmq, redis and etc.
brew install rabbitmq
start rabbitmq service
brew services start rabbitmq
double check if it is running probably
brew services list
Install result backend
For example, cassandra, sql, nosql is recommended
brew install redis
Install celery
pip install celery
Create a Python file tasks.py with following code
from celery import Celery
app = Celery('tasks', backend='redis://localhost/1', broker='amqp://localhost:5672')
CELERY_QUEUES = [
Queue('math', exchange=Exchange('course', type='direct'), routing_key='task'),
]
CELERY_ROUTES = {
'tasks.add': {
'queue': 'math',
'routing_key': 'task'
}
}
app.conf['CELERY_ROUTES'] = CELERY_ROUTES
app.conf['CELERY_QUEUES'] = CELERY_QUEUES
@app.task
def add(x, y):
return x + yStart worker
celery -A tasks worker -Q math --loglevel=info
Create a celery application to submit tasks
import tasks
task.add.delay(1, 2)
# or
task.add.apply_aync([1, 2])-
Messages are sent to exchanges.
-
An exchange routes messages to one or more queues.
-
he message waits in the queue until someone consumes it.
-
The message is deleted from the queue when it has been acknowledged.
-
Create an exchange
-
Create a queue
-
Bind the queue to the exchange.
Message consumer
Concurrency