Guest User

Untitled

a guest
May 24th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. from time import sleep
  2.  
  3. from celery import Celery
  4. from celery import group
  5.  
  6.  
  7. class Config:
  8. accept_content = ['pickle', 'json']
  9. result_serializer = 'pickle'
  10.  
  11.  
  12. app = Celery('tasks', backend='redis://localhost', broker='redis://localhost',
  13. config_source=Config)
  14.  
  15.  
  16. @app.task
  17. def add(x, y):
  18. return x + y
  19.  
  20.  
  21. @app.task
  22. def bad_add(x, y):
  23. raise Exception('Some exception')
  24.  
  25.  
  26. def print_result(result):
  27. print("Result id: {}".format(result.id))
  28. try:
  29. msg = result.ready()
  30. except Exception as e:
  31. msg = str(e)
  32.  
  33. print("Result ready: {}".format(msg))
  34.  
  35. print("Parent id: {}".format(result.parent.id))
  36.  
  37. try:
  38. msg = result.parent.ready()
  39. info = result.parent.info
  40. except Exception as e:
  41. msg = str(e)
  42. info = 'N/A'
  43.  
  44. print("Parent ready: {}".format(msg))
  45. print("Parent info: {}".format(info))
  46.  
  47. counter = 1
  48. for c in result.children:
  49. print("Child {} id: {}".format(counter, c.id))
  50.  
  51. try:
  52. msg = c.ready()
  53. info = c.info
  54. except Exception as e:
  55. msg = str(e)
  56. info = 'N/A'
  57.  
  58. print("Child {} ready: {}".format(counter, msg))
  59. print("Child {} info: {}".format(counter, info))
  60. counter += 1
  61.  
  62.  
  63. if __name__ == "__main__":
  64. print("Everything is ok")
  65. result = (add.si(1, 2) | group(add.si(3, 4), add.si(5, 6))).delay()
  66. # delay + sleep instead of get, because get fails saying it needs an argument
  67. sleep(1)
  68. print_result(result)
  69. result.save()
  70.  
  71. print("\nOne of the group tasks fail")
  72. result = (add.si(1, 2) | group(bad_add.si(3, 4), add.si(5, 6))).delay()
  73. sleep(1)
  74. print_result(result)
  75. result.save()
  76.  
  77. print("\nThe first task fails")
  78. result = (bad_add.si(1, 2) | group(add.si(3, 4), add.si(5, 6))).delay()
  79. sleep(1)
  80. print_result(result)
  81. result.save()
Add Comment
Please, Sign In to add comment