Skip to content

Instantly share code, notes, and snippets.

@sunshineatnoon
Created May 11, 2016 03:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunshineatnoon/778edbbfb1707a6bf6bc897a79eff70f to your computer and use it in GitHub Desktop.
Save sunshineatnoon/778edbbfb1707a6bf6bc897a79eff70f to your computer and use it in GitHub Desktop.
A memo on learning python library Celery
  1. Install Celery and RabbitMQ on MAC:
pip install celery
brew install rabbitmq
  1. Basic

    Create folder celery_try and create file tasks.py. In this tasks.py, we define a Celery task:

    app = Celery('tasks', backend="amqp", broker='amqp://guest@localhost:5672//')
    

    backend is required if we want to extract results back. Then define a task:

    @app.task
    

def add(x, y): print 'hello celery' time.sleep(10) return x + y

Then start our celery worker:

celery worker -A tasks --loglevel=INFO

And test if we are able to assign tasks to the worker:

$python

from tasks import add result = add.delay(3,5) result.ready() False result.state 'PENDING' result.state 'SUCCESS' result.ready() True result.get() 8


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment