canvas.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. # -*- coding: utf-8 -*-
  2. """
  3. celery.canvas
  4. ~~~~~~~~~~~~~
  5. Composing task workflows.
  6. Documentation for these functions are in :mod:`celery`.
  7. You should not import from this module directly.
  8. """
  9. from __future__ import absolute_import
  10. from copy import deepcopy
  11. from functools import partial as _partial, reduce
  12. from operator import itemgetter
  13. from itertools import chain as _chain
  14. from kombu.utils import cached_property, fxrange, kwdict, reprcall, uuid
  15. from celery import current_app
  16. from celery.local import Proxy
  17. from celery.result import AsyncResult, GroupResult
  18. from celery.utils.functional import (
  19. maybe_list, is_list, regen,
  20. chunks as _chunks,
  21. )
  22. from celery.utils.text import truncate
  23. Chord = Proxy(lambda: current_app.tasks['celery.chord'])
  24. class _getitem_property(object):
  25. """Attribute -> dict key descriptor.
  26. The target object must support ``__getitem__``,
  27. and optionally ``__setitem__``.
  28. Example:
  29. class Me(dict):
  30. deep = defaultdict(dict)
  31. foo = _getitem_property('foo')
  32. deep_thing = _getitem_property('deep.thing')
  33. >>> me = Me()
  34. >>> me.foo
  35. None
  36. >>> me.foo = 10
  37. >>> me.foo
  38. 10
  39. >>> me['foo']
  40. 10
  41. >>> me.deep_thing = 42
  42. >>> me.deep_thinge
  43. 42
  44. >>> me.deep:
  45. defaultdict(<type 'dict'>, {'thing': 42})
  46. """
  47. def __init__(self, keypath):
  48. path, _, self.key = keypath.rpartition('.')
  49. self.path = path.split('.') if path else None
  50. def _path(self, obj):
  51. return (reduce(lambda d, k: d[k], [obj] + self.path) if self.path
  52. else obj)
  53. def __get__(self, obj, type=None):
  54. if obj is None:
  55. return type
  56. return self._path(obj).get(self.key)
  57. def __set__(self, obj, value):
  58. self._path(obj)[self.key] = value
  59. class Signature(dict):
  60. """Class that wraps the arguments and execution options
  61. for a single task invocation.
  62. Used as the parts in a :class:`group` or to safely
  63. pass tasks around as callbacks.
  64. :param task: Either a task class/instance, or the name of a task.
  65. :keyword args: Positional arguments to apply.
  66. :keyword kwargs: Keyword arguments to apply.
  67. :keyword options: Additional options to :meth:`Task.apply_async`.
  68. Note that if the first argument is a :class:`dict`, the other
  69. arguments will be ignored and the values in the dict will be used
  70. instead.
  71. >>> s = subtask('tasks.add', args=(2, 2))
  72. >>> subtask(s)
  73. {'task': 'tasks.add', args=(2, 2), kwargs={}, options={}}
  74. """
  75. TYPES = {}
  76. _type = None
  77. @classmethod
  78. def register_type(cls, subclass, name=None):
  79. cls.TYPES[name or subclass.__name__] = subclass
  80. return subclass
  81. @classmethod
  82. def from_dict(self, d):
  83. typ = d.get('subtask_type')
  84. if typ:
  85. return self.TYPES[typ].from_dict(kwdict(d))
  86. return Signature(d)
  87. def __init__(self, task=None, args=None, kwargs=None, options=None,
  88. type=None, subtask_type=None, immutable=False, **ex):
  89. init = dict.__init__
  90. if isinstance(task, dict):
  91. return init(self, task) # works like dict(d)
  92. # Also supports using task class/instance instead of string name.
  93. try:
  94. task_name = task.name
  95. except AttributeError:
  96. task_name = task
  97. else:
  98. self._type = task
  99. init(self,
  100. task=task_name, args=tuple(args or ()),
  101. kwargs=kwargs or {},
  102. options=dict(options or {}, **ex),
  103. subtask_type=subtask_type,
  104. immutable=immutable)
  105. def __call__(self, *partial_args, **partial_kwargs):
  106. return self.apply_async(partial_args, partial_kwargs)
  107. delay = __call__
  108. def apply(self, args=(), kwargs={}, **options):
  109. """Apply this task locally."""
  110. # For callbacks: extra args are prepended to the stored args.
  111. args, kwargs, options = self._merge(args, kwargs, options)
  112. return self.type.apply(args, kwargs, **options)
  113. def _merge(self, args=(), kwargs={}, options={}):
  114. if self.immutable:
  115. return self.args, self.kwargs, dict(self.options, **options)
  116. return (tuple(args) + tuple(self.args) if args else self.args,
  117. dict(self.kwargs, **kwargs) if kwargs else self.kwargs,
  118. dict(self.options, **options) if options else self.options)
  119. def clone(self, args=(), kwargs={}, **opts):
  120. # need to deepcopy options so origins links etc. is not modified.
  121. args, kwargs, opts = self._merge(args, kwargs, opts)
  122. s = Signature.from_dict({'task': self.task, 'args': tuple(args),
  123. 'kwargs': kwargs, 'options': deepcopy(opts),
  124. 'subtask_type': self.subtask_type,
  125. 'immutable': self.immutable})
  126. s._type = self._type
  127. return s
  128. partial = clone
  129. def _freeze(self, _id=None):
  130. opts = self.options
  131. try:
  132. tid = opts['task_id']
  133. except KeyError:
  134. tid = opts['task_id'] = _id or uuid()
  135. return self.AsyncResult(tid)
  136. def replace(self, args=None, kwargs=None, options=None):
  137. s = self.clone()
  138. if args is not None:
  139. s.args = args
  140. if kwargs is not None:
  141. s.kwargs = kwargs
  142. if options is not None:
  143. s.options = options
  144. return s
  145. def set(self, immutable=None, **options):
  146. if immutable is not None:
  147. self.immutable = immutable
  148. self.options.update(options)
  149. return self
  150. def apply_async(self, args=(), kwargs={}, **options):
  151. # For callbacks: extra args are prepended to the stored args.
  152. args, kwargs, options = self._merge(args, kwargs, options)
  153. return self._apply_async(args, kwargs, **options)
  154. def append_to_list_option(self, key, value):
  155. items = self.options.setdefault(key, [])
  156. if value not in items:
  157. items.append(value)
  158. return value
  159. def link(self, callback):
  160. return self.append_to_list_option('link', callback)
  161. def link_error(self, errback):
  162. return self.append_to_list_option('link_error', errback)
  163. def flatten_links(self):
  164. return list(_chain.from_iterable(_chain(
  165. [[self]],
  166. (link.flatten_links()
  167. for link in maybe_list(self.options.get('link')) or [])
  168. )))
  169. def __or__(self, other):
  170. if not isinstance(self, chain) and isinstance(other, chain):
  171. return chain((self,) + other.tasks)
  172. elif isinstance(other, chain):
  173. return chain(*self.tasks + other.tasks)
  174. elif isinstance(other, Signature):
  175. if isinstance(self, chain):
  176. return chain(*self.tasks + (other, ))
  177. return chain(self, other)
  178. return NotImplemented
  179. def __deepcopy__(self, memo):
  180. memo[id(self)] = self
  181. return dict(self)
  182. def __invert__(self):
  183. return self.apply_async().get()
  184. def __reduce__(self):
  185. # for serialization, the task type is lazily loaded,
  186. # and not stored in the dict itself.
  187. return subtask, (dict(self), )
  188. def reprcall(self, *args, **kwargs):
  189. args, kwargs, _ = self._merge(args, kwargs, {})
  190. return reprcall(self['task'], args, kwargs)
  191. def election(self):
  192. type = self.type
  193. app = type.app
  194. tid = self.options.get('task_id') or uuid()
  195. with app.producer_or_acquire(None) as P:
  196. props = type.backend.on_task_call(P, tid)
  197. app.control.election(tid, 'task', self.clone(task_id=tid, **props),
  198. connection=P.connection)
  199. return type.AsyncResult(tid)
  200. def __repr__(self):
  201. return self.reprcall()
  202. @cached_property
  203. def type(self):
  204. return self._type or current_app.tasks[self['task']]
  205. @cached_property
  206. def AsyncResult(self):
  207. try:
  208. return self.type.AsyncResult
  209. except KeyError: # task not registered
  210. return AsyncResult
  211. @cached_property
  212. def _apply_async(self):
  213. try:
  214. return self.type.apply_async
  215. except KeyError:
  216. return _partial(current_app.send_task, self['task'])
  217. id = _getitem_property('options.task_id')
  218. task = _getitem_property('task')
  219. args = _getitem_property('args')
  220. kwargs = _getitem_property('kwargs')
  221. options = _getitem_property('options')
  222. subtask_type = _getitem_property('subtask_type')
  223. immutable = _getitem_property('immutable')
  224. class chain(Signature):
  225. def __init__(self, *tasks, **options):
  226. tasks = tasks[0] if len(tasks) == 1 and is_list(tasks[0]) else tasks
  227. Signature.__init__(
  228. self, 'celery.chain', (), {'tasks': tasks}, **options
  229. )
  230. self.tasks = tasks
  231. self.subtask_type = 'chain'
  232. def __call__(self, *args, **kwargs):
  233. return self.apply_async(args, kwargs)
  234. @classmethod
  235. def from_dict(self, d):
  236. tasks = d['kwargs']['tasks']
  237. if d['args'] and tasks:
  238. # partial args passed on to first task in chain (Issue #1057).
  239. tasks[0]['args'] = d['args'] + tasks[0]['args']
  240. return chain(*d['kwargs']['tasks'], **kwdict(d['options']))
  241. def __repr__(self):
  242. return ' | '.join(map(repr, self.tasks))
  243. Signature.register_type(chain)
  244. class _basemap(Signature):
  245. _task_name = None
  246. _unpack_args = itemgetter('task', 'it')
  247. def __init__(self, task, it, **options):
  248. Signature.__init__(
  249. self, self._task_name, (),
  250. {'task': task, 'it': regen(it)}, immutable=True, **options
  251. )
  252. def apply_async(self, args=(), kwargs={}, **opts):
  253. # need to evaluate generators
  254. task, it = self._unpack_args(self.kwargs)
  255. return self.type.apply_async(
  256. (), {'task': task, 'it': list(it)}, **opts
  257. )
  258. @classmethod
  259. def from_dict(self, d):
  260. return chunks(*self._unpack_args(d['kwargs']), **d['options'])
  261. class xmap(_basemap):
  262. _task_name = 'celery.map'
  263. def __repr__(self):
  264. task, it = self._unpack_args(self.kwargs)
  265. return '[{0}(x) for x in {1}]'.format(task.task,
  266. truncate(repr(it), 100))
  267. Signature.register_type(xmap)
  268. class xstarmap(_basemap):
  269. _task_name = 'celery.starmap'
  270. def __repr__(self):
  271. task, it = self._unpack_args(self.kwargs)
  272. return '[{0}(*x) for x in {1}]'.format(task.task,
  273. truncate(repr(it), 100))
  274. Signature.register_type(xstarmap)
  275. class chunks(Signature):
  276. _unpack_args = itemgetter('task', 'it', 'n')
  277. def __init__(self, task, it, n, **options):
  278. Signature.__init__(
  279. self, 'celery.chunks', (),
  280. {'task': task, 'it': regen(it), 'n': n},
  281. immutable=True, **options
  282. )
  283. @classmethod
  284. def from_dict(self, d):
  285. return chunks(*self._unpack_args(d['kwargs']), **d['options'])
  286. def apply_async(self, args=(), kwargs={}, **opts):
  287. return self.group().apply_async(args, kwargs, **opts)
  288. def __call__(self, **options):
  289. return self.group()(**options)
  290. def group(self):
  291. # need to evaluate generators
  292. task, it, n = self._unpack_args(self.kwargs)
  293. return group(xstarmap(task, part) for part in _chunks(iter(it), n))
  294. @classmethod
  295. def apply_chunks(cls, task, it, n):
  296. return cls(task, it, n)()
  297. Signature.register_type(chunks)
  298. def _maybe_group(tasks):
  299. if isinstance(tasks, group):
  300. tasks = list(tasks.tasks)
  301. elif isinstance(tasks, Signature):
  302. tasks = [tasks]
  303. else:
  304. tasks = regen(tasks)
  305. return tasks
  306. class group(Signature):
  307. def __init__(self, *tasks, **options):
  308. if len(tasks) == 1:
  309. tasks = _maybe_group(tasks[0])
  310. Signature.__init__(
  311. self, 'celery.group', (), {'tasks': tasks}, **options
  312. )
  313. self.tasks, self.subtask_type = tasks, 'group'
  314. @classmethod
  315. def from_dict(self, d):
  316. tasks = d['kwargs']['tasks']
  317. if d['args'] and tasks:
  318. # partial args passed on to all tasks in the group (Issue #1057).
  319. for task in tasks:
  320. task['args'] = d['args'] + task['args']
  321. return group(tasks, **kwdict(d['options']))
  322. def __call__(self, *partial_args, **options):
  323. tasks, result, gid, args = self.type.prepare(
  324. options, [Signature.clone(t) for t in self.tasks], partial_args,
  325. )
  326. return self.type(tasks, result, gid, args)
  327. def _freeze(self, _id=None):
  328. opts = self.options
  329. try:
  330. gid = opts['group']
  331. except KeyError:
  332. gid = opts['group'] = uuid()
  333. new_tasks, results = [], []
  334. for task in self.tasks:
  335. task = maybe_subtask(task).clone()
  336. results.append(task._freeze())
  337. new_tasks.append(task)
  338. self.tasks = self.kwargs['tasks'] = new_tasks
  339. return GroupResult(gid, results)
  340. def skew(self, start=1.0, stop=None, step=1.0):
  341. it = fxrange(start, stop, step, repeatlast=True)
  342. for task in self.tasks:
  343. task.set(countdown=next(it))
  344. return self
  345. def __iter__(self):
  346. return iter(self.tasks)
  347. def __repr__(self):
  348. return repr(self.tasks)
  349. Signature.register_type(group)
  350. class chord(Signature):
  351. Chord = Chord
  352. def __init__(self, header, body=None, task='celery.chord',
  353. args=(), kwargs={}, **options):
  354. Signature.__init__(
  355. self, task, args,
  356. dict(kwargs, header=_maybe_group(header),
  357. body=maybe_subtask(body)), **options
  358. )
  359. self.subtask_type = 'chord'
  360. @classmethod
  361. def from_dict(self, d):
  362. args, d['kwargs'] = self._unpack_args(**kwdict(d['kwargs']))
  363. return self(*args, **kwdict(d))
  364. @staticmethod
  365. def _unpack_args(header=None, body=None, **kwargs):
  366. # Python signatures are better at extracting keys from dicts
  367. # than manually popping things off.
  368. return (header, body), kwargs
  369. def __call__(self, body=None, **kwargs):
  370. _chord = self.Chord
  371. body = (body or self.kwargs['body']).clone()
  372. kwargs = dict(self.kwargs, body=body, **kwargs)
  373. if _chord.app.conf.CELERY_ALWAYS_EAGER:
  374. return self.apply((), kwargs)
  375. callback_id = body.options.setdefault('task_id', uuid())
  376. _chord(**kwargs)
  377. return _chord.AsyncResult(callback_id)
  378. def clone(self, *args, **kwargs):
  379. s = Signature.clone(self, *args, **kwargs)
  380. # need to make copy of body
  381. try:
  382. s.kwargs['body'] = s.kwargs['body'].clone()
  383. except (AttributeError, KeyError):
  384. pass
  385. return s
  386. def link(self, callback):
  387. self.body.link(callback)
  388. return callback
  389. def link_error(self, errback):
  390. self.body.link_error(errback)
  391. return errback
  392. def __repr__(self):
  393. if self.body:
  394. return self.body.reprcall(self.tasks)
  395. return '<chord without body: {0.tasks!r}>'.format(self)
  396. @property
  397. def tasks(self):
  398. return self.kwargs['header']
  399. @property
  400. def body(self):
  401. return self.kwargs.get('body')
  402. Signature.register_type(chord)
  403. def subtask(varies, *args, **kwargs):
  404. if not (args or kwargs) and isinstance(varies, dict):
  405. if isinstance(varies, Signature):
  406. return varies.clone()
  407. return Signature.from_dict(varies)
  408. return Signature(varies, *args, **kwargs)
  409. def maybe_subtask(d):
  410. if d is not None and isinstance(d, dict) and not isinstance(d, Signature):
  411. return subtask(d)
  412. return d