Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from time import sleep
- from celery import Celery
- from celery import group
- class Config:
- accept_content = ['pickle', 'json']
- result_serializer = 'pickle'
- app = Celery('tasks', backend='redis://localhost', broker='redis://localhost',
- config_source=Config)
- @app.task
- def add(x, y):
- return x + y
- @app.task
- def bad_add(x, y):
- raise Exception('Some exception')
- def print_result(result):
- print("Result id: {}".format(result.id))
- try:
- msg = result.ready()
- except Exception as e:
- msg = str(e)
- print("Result ready: {}".format(msg))
- print("Parent id: {}".format(result.parent.id))
- try:
- msg = result.parent.ready()
- info = result.parent.info
- except Exception as e:
- msg = str(e)
- info = 'N/A'
- print("Parent ready: {}".format(msg))
- print("Parent info: {}".format(info))
- counter = 1
- for c in result.children:
- print("Child {} id: {}".format(counter, c.id))
- try:
- msg = c.ready()
- info = c.info
- except Exception as e:
- msg = str(e)
- info = 'N/A'
- print("Child {} ready: {}".format(counter, msg))
- print("Child {} info: {}".format(counter, info))
- counter += 1
- if __name__ == "__main__":
- print("Everything is ok")
- result = (add.si(1, 2) | group(add.si(3, 4), add.si(5, 6))).delay()
- # delay + sleep instead of get, because get fails saying it needs an argument
- sleep(1)
- print_result(result)
- result.save()
- print("\nOne of the group tasks fail")
- result = (add.si(1, 2) | group(bad_add.si(3, 4), add.si(5, 6))).delay()
- sleep(1)
- print_result(result)
- result.save()
- print("\nThe first task fails")
- result = (bad_add.si(1, 2) | group(add.si(3, 4), add.si(5, 6))).delay()
- sleep(1)
- print_result(result)
- result.save()
Add Comment
Please, Sign In to add comment