Skip to content

Instantly share code, notes, and snippets.

@drmalex07
Last active August 31, 2023 03:53
Show Gist options
  • Save drmalex07/83eafd793fd8f10c1ee9 to your computer and use it in GitHub Desktop.
Save drmalex07/83eafd793fd8f10c1ee9 to your computer and use it in GitHub Desktop.
A quickstart example with celery queue. #celery
# This is a quickstart! In the real world use a real broker (message queue)
# such as Redis or RabbitMQ !!
BROKER_URL = 'sqlalchemy+sqlite:///tasks.db'
CELERY_RESULT_BACKEND = "db+sqlite:///results.db"
CELERY_IMPORTS = ['tasks']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_ACCEPT_CONTENT = ['json', 'yaml']
from celery import Celery
app1 = Celery('tasks')
app1.config_from_object('celeryconfig')
# Send a simple task (create and send in 1 step)
res = app1.send_task('foo.adda', args=(1,8,2,-3,5), countdown=1)
print '%r: Got %s' %(res, res.get(timeout=5))
# Better: send a task using a uniform delay/get pattern.
# Essentially, it creates a subtask i.e a task signature
t = app1.signature('foo.adda', args=(1,8,2,-3,5))
res = t.delay()
print '%r: Got %s' %(res, res.get(timeout=5))
# Send a long-running task
res = app1.send_task('foo.waste_time', args=(5,))
print '%r: This will waste some time' %(res)
from celery import Celery
from celery.task import task, subtask
from celery.utils.log import get_task_logger
import time
log1 = get_task_logger(__name__)
@task(name='foo.add')
def add(x, y):
log1.info ('Adding %.2f and %.2f' %(x,y))
# calculate and return
return x + y
@task(name='foo.adda')
def adda(*args):
'''A simple task adding all arguments'''
log1.info('Adding numbers ' + repr(args))
return sum(args)
@task(name='foo.addi')
def addi(it):
'''A simple task adding all items from iterable'''
log1.info('Adding numbers ' + repr(it))
return sum(it)
@task(name='foo.mul')
def mul(x, y):
log1.info('Multiplying %.2f and %.2f' %(x,y))
return x * y
@task(name='foo.waste_time')
def waste_time(n=12, callback=None):
for i in range(0, n):
log1.info('Wasting some time (%d/%d)' % (i, n))
time.sleep(5)
if callback:
log1.info('Finished task: About to invoke %r' % (callback))
subtask(callback).delay()
else:
log1.info('Finished task')
return {'status': 'wasted'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment