Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sets.py 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # -*- coding: utf-8 -*-
  2. """
  3. celery.task.sets
  4. ~~~~~~~~~~~~~~~~
  5. Old ``group`` implementation, this module should
  6. not be used anymore use :func:`celery.group` instead.
  7. """
  8. from __future__ import absolute_import
  9. from celery._state import get_current_worker_task
  10. from celery.app import app_or_default
  11. from celery.canvas import maybe_signature # noqa
  12. from celery.utils import uuid, warn_deprecated
  13. from celery.canvas import subtask # noqa
  14. warn_deprecated(
  15. 'celery.task.sets and TaskSet', removal='4.0',
  16. alternative="""\
  17. Please use "group" instead (see the Canvas section in the userguide)\
  18. """)
  19. class TaskSet(list):
  20. """A task containing several subtasks, making it possible
  21. to track how many, or when all of the tasks have been completed.
  22. :param tasks: A list of :class:`subtask` instances.
  23. Example::
  24. >>> from myproj.tasks import refresh_feed
  25. >>> urls = ('http://cnn.com/rss', 'http://bbc.co.uk/rss')
  26. >>> s = TaskSet(refresh_feed.s(url) for url in urls)
  27. >>> taskset_result = s.apply_async()
  28. >>> list_of_return_values = taskset_result.join() # *expensive*
  29. """
  30. app = None
  31. def __init__(self, tasks=None, app=None, Publisher=None):
  32. self.app = app_or_default(app or self.app)
  33. super(TaskSet, self).__init__(
  34. maybe_signature(t, app=self.app) for t in tasks or []
  35. )
  36. self.Publisher = Publisher or self.app.amqp.TaskProducer
  37. self.total = len(self) # XXX compat
  38. def apply_async(self, connection=None, publisher=None, taskset_id=None):
  39. """Apply TaskSet."""
  40. app = self.app
  41. if app.conf.CELERY_ALWAYS_EAGER:
  42. return self.apply(taskset_id=taskset_id)
  43. with app.connection_or_acquire(connection) as conn:
  44. setid = taskset_id or uuid()
  45. pub = publisher or self.Publisher(conn)
  46. results = self._async_results(setid, pub)
  47. result = app.TaskSetResult(setid, results)
  48. parent = get_current_worker_task()
  49. if parent:
  50. parent.add_trail(result)
  51. return result
  52. def _async_results(self, taskset_id, publisher):
  53. return [task.apply_async(taskset_id=taskset_id, publisher=publisher)
  54. for task in self]
  55. def apply(self, taskset_id=None):
  56. """Applies the TaskSet locally by blocking until all tasks return."""
  57. setid = taskset_id or uuid()
  58. return self.app.TaskSetResult(setid, self._sync_results(setid))
  59. def _sync_results(self, taskset_id):
  60. return [task.apply(taskset_id=taskset_id) for task in self]
  61. @property
  62. def tasks(self):
  63. return self
  64. @tasks.setter # noqa
  65. def tasks(self, tasks):
  66. self[:] = tasks