Skip to content

Instantly share code, notes, and snippets.

@mitgr81
Created July 12, 2013 21:03
Show Gist options
  • Save mitgr81/5987828 to your computer and use it in GitHub Desktop.
Save mitgr81/5987828 to your computer and use it in GitHub Desktop.
Just a bit of playing with Celery to see how to schedule tasks for the FUTURE!
from tasks import add
result = add.delay(425, 554)
result.ready()
# OUT: False
result.get()
# OUT: 979
result = add.apply_async((425, 554), countdown=3)
result.ready()
# OUT: False
result.ready()
# OUT: True
result.get()
# OUT: 979
result = add.apply_async((425, 554), countdown=30)
result.ready()
# OUT: False
result.ready()
# OUT: False
result.ready()
# OUT: False
result.ready()
# OUT: False
result.ready()
# OUT: True
result.get()
# OUT: 979
from datetime import datetime, timedelta
onemin = datetime.utcnow() + timedelta(minutes=1)
result = add.apply_async((425, 554), eta=onemin)
result.ready()
# OUT: True
result.get()
# OUT: 979
result = add.apply_async((425, 554), eta=datetime.utcnow() + timedelta(seconds=5))
result.ready()
# OUT: False
result.ready()
# OUT: True
result.get()
# OUT: 979
# NOTE: Here I shut down the Celery worker
result = add.apply_async((425, 554), eta=datetime.utcnow() + timedelta(seconds=5))
result.ready()
# OUT: False
result.ready()
# NOTE: Here I started up the Celery worker
# OUT: False
result.ready()
# OUT: False
result.ready()
# OUT: True
result.get()
# OUT: 979
from celery import Celery
mq_pass = 'p@ssw3rd'
mq_user = 'iamarobot'
celery = Celery('cmcgtasks', backend='amqp', broker='amqp://{}:{}@mq.example.com'.format(mq_user, mq_pass))
@celery.task
def add(x, y):
return x + y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment