Skip to content

Instantly share code, notes, and snippets.

@nezaidu
Forked from taylorhughes/run-worker.sh
Created September 24, 2015 10:43
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 nezaidu/611d43ffc0eb0e66390e to your computer and use it in GitHub Desktop.
Save nezaidu/611d43ffc0eb0e66390e to your computer and use it in GitHub Desktop.
Celery example with tasks of varying length + -Ofair
# tested with celery[redis]==3.1.17
# to run with default configuration -- tasks will take 14 seconds to complete the 20 tasks in start_all() below
celery worker -A cluster_project.celery_app -Q tester -lINFO --concurrency=4
# to run with -Ofair -- tasks will take 10 seconds to complete
celery worker -A cluster_project.celery_app -Q tester -lINFO --concurrency=4 -Ofair
import time
from cluster_project import celery_app
# To run this contrived example:
# 1. start worker with -Ofair or not
# 2. call start_all() and watch celery worker output
def start_all():
slow_task.delay()
for i in range(19):
fast_task.delay(i)
@celery_app.task(queue='tester')
def slow_task():
print 'starting slow task'
time.sleep(10)
print 'done with slow task'
@celery_app.task(queue='tester')
def fast_task(i):
print 'starting fast task %d' % i
time.sleep(1)
print 'done with fast task %d' % i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment