Last active
January 15, 2025 19:33
-
-
Save taylorhughes/d5318101dc0fbf96ecdb to your computer and use it in GitHub Desktop.
Celery example with tasks of varying length + -Ofair
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
Might be nice to change the imports as follows so it's easier for anyone to run:
from celery import Celery
celery_app = Celery('tasks', broker='redis://localhost')
and run as celery worker -A tasks ...
Starting version 4.0 this behavior is by defult - http://docs.celeryproject.org/en/4.0/whatsnew-4.0.html#ofair-is-now-the-default-scheduling-strategy.
@w00lf thanks, was curious why it wasn't in v4 docs
Starting version 4.0 this behavior is by default - http://docs.celeryproject.org/en/4.0/whatsnew-4.0.html#ofair-is-now-the-default-scheduling-strategy.
Sweet!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What is @celery_app here. Can you share it also. it would be great for me.