Skip to content

dongliang3571/celery-app-setup

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 

Repository files navigation

celery-app-setup

Workflow

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 + y

Start 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])
  1. Messages are sent to exchanges.

  2. An exchange routes messages to one or more queues.

  3. he message waits in the queue until someone consumes it.

  4. The message is deleted from the queue when it has been acknowledged.

  5. Create an exchange

  6. Create a queue

  7. Bind the queue to the exchange.

Message consumer

Concurrency

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors