__init__.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. """
  2. Working with tasks and task sets.
  3. """
  4. import warnings
  5. from celery.serialization import pickle
  6. from celery.task.base import Task, PeriodicTask
  7. from celery.task.sets import TaskSet
  8. from celery.task.builtins import PingTask, ExecuteRemoteTask
  9. from celery.task.builtins import AsynchronousMapTask, _dmap
  10. from celery.task.control import discard_all
  11. from celery.task.http import HttpDispatchTask
  12. __all__ = ["Task", "TaskSet", "PeriodicTask", "discard_all",
  13. "dmap", "dmap_async", "execute_remote", "HttpDispatchTask"]
  14. def dmap(fun, args, timeout=None):
  15. """Distribute processing of the arguments and collect the results.
  16. Example
  17. >>> from celery.task import dmap
  18. >>> import operator
  19. >>> dmap(operator.add, [[2, 2], [4, 4], [8, 8]])
  20. [4, 8, 16]
  21. """
  22. return _dmap(fun, args, timeout)
  23. def dmap_async(fun, args, timeout=None):
  24. """Distribute processing of the arguments and collect the results
  25. asynchronously.
  26. :returns :class:`celery.result.AsyncResult`:
  27. Example
  28. >>> from celery.task import dmap_async
  29. >>> import operator
  30. >>> presult = dmap_async(operator.add, [[2, 2], [4, 4], [8, 8]])
  31. >>> presult
  32. <AsyncResult: 373550e8-b9a0-4666-bc61-ace01fa4f91d>
  33. >>> presult.status
  34. 'SUCCESS'
  35. >>> presult.result
  36. [4, 8, 16]
  37. """
  38. return AsynchronousMapTask.delay(pickle.dumps(fun), args, timeout=timeout)
  39. def execute_remote(fun, *args, **kwargs):
  40. """Execute arbitrary function/object remotely.
  41. :param fun: A callable function or object.
  42. :param \*args: Positional arguments to apply to the function.
  43. :param \*\*kwargs: Keyword arguments to apply to the function.
  44. The object must be picklable, so you can't use lambdas or functions
  45. defined in the REPL (the objects must have an associated module).
  46. :returns class:`celery.result.AsyncResult`:
  47. """
  48. return ExecuteRemoteTask.delay(pickle.dumps(fun), args, kwargs)
  49. def ping():
  50. """Deprecated and scheduled for removal in Celery 2.3.
  51. Please use :meth:`celery.task.control.ping` instead.
  52. """
  53. warnings.warn(DeprecationWarning(
  54. "The ping task has been deprecated and will be removed in Celery "
  55. "v2.3. Please use inspect.ping instead."))
  56. return PingTask.apply_async().get()