Skip to content

Instantly share code, notes, and snippets.

@adikrishnan
Last active May 30, 2018 15:03
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 adikrishnan/064ed8d1fadccc0018e33244ce8f21b0 to your computer and use it in GitHub Desktop.
Save adikrishnan/064ed8d1fadccc0018e33244ce8f21b0 to your computer and use it in GitHub Desktop.
Part of the blog post - Playing with Chords - Celery which will walk through how to use "chords" structure in Celery - https://adikrishnan.in/2018/05/30/playing-with-chords-celery/
# Refer https://adikrishnan.in/2018/05/30/playing-with-chords-celery/ for understanding the concept.
from tasks import hello, chord_service, non_chord_service
hello.delay()
chord_service.delay()
non_chord_service.delay()
# Refer https://adikrishnan.in/2018/05/30/playing-with-chords-celery/ for understanding the concept.
import time
import datetime
import subprocess
from celery import Celery, chord
conf = {
"broker_url": "redis://localhost:6379/7",
"result_backend": "redis://localhost:6379/7",
"result_expires": "600"
}
celery_app = Celery()
celery_app.config_from_object(conf)
NUM_OF_ITEMS = 5
@celery_app.task
def hello():
print("Bello Merld at {}".format(datetime.datetime.now().isoformat()))
@celery_app.task
def run_ping(item_num, func_name):
start = time.time()
cmd = "ping adikrishnan.in -c 4"
res = subprocess.check_output(cmd.split(), universal_newlines=True)
end = time.time()
print("Item Number: {}, Function Name: {}, Total time: {:.2f} sec".format(item_num, func_name,
end - start))
return res
@celery_app.task
def chord_service():
start = time.time()
chord(run_ping.s(i, chord_service.__name__) for i in range(5))(chord_result.s())
end = time.time()
print("{} ended, Total time: {:.2f} sec".format(chord_service.__name__, end - start))
@celery_app.task
def chord_result(res):
print(res)
@celery_app.task
def non_chord_service():
start = time.time()
res_all = [ run_ping(i, non_chord_service.__name__) for i in range(NUM_OF_ITEMS) ]
end = time.time()
print(res_all)
print("{} ended, Total time: {:.2f} sec".format(non_chord_service.__name__, end - start))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment