canvas.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. # -*- coding: utf-8 -*-
  2. """
  3. celery.canvas
  4. ~~~~~~~~~~~~~
  5. Composing task workflows.
  6. Documentation for some of these types are in :mod:`celery`.
  7. You should import these from :mod:`celery` and not this module.
  8. """
  9. from __future__ import absolute_import
  10. from collections import MutableSequence
  11. from copy import deepcopy
  12. from functools import partial as _partial, reduce
  13. from operator import itemgetter
  14. from itertools import chain as _chain
  15. from kombu.utils import cached_property, fxrange, kwdict, reprcall, uuid
  16. from celery._state import current_app
  17. from celery.utils.functional import (
  18. maybe_list, is_list, regen,
  19. chunks as _chunks,
  20. )
  21. from celery.utils.text import truncate
  22. __all__ = ['Signature', 'chain', 'xmap', 'xstarmap', 'chunks',
  23. 'group', 'chord', 'signature', 'maybe_signature']
  24. class _getitem_property(object):
  25. """Attribute -> dict key descriptor.
  26. The target object must support ``__getitem__``,
  27. and optionally ``__setitem__``.
  28. Example:
  29. >>> from collections import defaultdict
  30. >>> class Me(dict):
  31. ... deep = defaultdict(dict)
  32. ...
  33. ... foo = _getitem_property('foo')
  34. ... deep_thing = _getitem_property('deep.thing')
  35. >>> me = Me()
  36. >>> me.foo
  37. None
  38. >>> me.foo = 10
  39. >>> me.foo
  40. 10
  41. >>> me['foo']
  42. 10
  43. >>> me.deep_thing = 42
  44. >>> me.deep_thing
  45. 42
  46. >>> me.deep
  47. defaultdict(<type 'dict'>, {'thing': 42})
  48. """
  49. def __init__(self, keypath):
  50. path, _, self.key = keypath.rpartition('.')
  51. self.path = path.split('.') if path else None
  52. def _path(self, obj):
  53. return (reduce(lambda d, k: d[k], [obj] + self.path) if self.path
  54. else obj)
  55. def __get__(self, obj, type=None):
  56. if obj is None:
  57. return type
  58. return self._path(obj).get(self.key)
  59. def __set__(self, obj, value):
  60. self._path(obj)[self.key] = value
  61. def maybe_unroll_group(g):
  62. """Unroll group with only one member."""
  63. # Issue #1656
  64. try:
  65. size = len(g.tasks)
  66. except TypeError:
  67. try:
  68. size = g.tasks.__length_hint__()
  69. except (AttributeError, TypeError):
  70. pass
  71. else:
  72. return list(g.tasks)[0] if size == 1 else g
  73. else:
  74. return g.tasks[0] if size == 1 else g
  75. class Signature(dict):
  76. """Class that wraps the arguments and execution options
  77. for a single task invocation.
  78. Used as the parts in a :class:`group` and other constructs,
  79. or to pass tasks around as callbacks while being compatible
  80. with serializers with a strict type subset.
  81. :param task: Either a task class/instance, or the name of a task.
  82. :keyword args: Positional arguments to apply.
  83. :keyword kwargs: Keyword arguments to apply.
  84. :keyword options: Additional options to :meth:`Task.apply_async`.
  85. Note that if the first argument is a :class:`dict`, the other
  86. arguments will be ignored and the values in the dict will be used
  87. instead.
  88. >>> s = signature('tasks.add', args=(2, 2))
  89. >>> signature(s)
  90. {'task': 'tasks.add', args=(2, 2), kwargs={}, options={}}
  91. """
  92. TYPES = {}
  93. _app = _type = None
  94. @classmethod
  95. def register_type(cls, subclass, name=None):
  96. cls.TYPES[name or subclass.__name__] = subclass
  97. return subclass
  98. @classmethod
  99. def from_dict(self, d, app=None):
  100. typ = d.get('subtask_type')
  101. if typ:
  102. return self.TYPES[typ].from_dict(kwdict(d), app=app)
  103. return Signature(d, app=app)
  104. def __init__(self, task=None, args=None, kwargs=None, options=None,
  105. type=None, subtask_type=None, immutable=False,
  106. app=None, **ex):
  107. self._app = app
  108. init = dict.__init__
  109. if isinstance(task, dict):
  110. return init(self, task) # works like dict(d)
  111. # Also supports using task class/instance instead of string name.
  112. try:
  113. task_name = task.name
  114. except AttributeError:
  115. task_name = task
  116. else:
  117. self._type = task
  118. init(self,
  119. task=task_name, args=tuple(args or ()),
  120. kwargs=kwargs or {},
  121. options=dict(options or {}, **ex),
  122. subtask_type=subtask_type,
  123. immutable=immutable)
  124. def __call__(self, *partial_args, **partial_kwargs):
  125. args, kwargs, _ = self._merge(partial_args, partial_kwargs, None)
  126. return self.type(*args, **kwargs)
  127. def delay(self, *partial_args, **partial_kwargs):
  128. return self.apply_async(partial_args, partial_kwargs)
  129. def apply(self, args=(), kwargs={}, **options):
  130. """Apply this task locally."""
  131. # For callbacks: extra args are prepended to the stored args.
  132. args, kwargs, options = self._merge(args, kwargs, options)
  133. return self.type.apply(args, kwargs, **options)
  134. def _merge(self, args=(), kwargs={}, options={}):
  135. if self.immutable:
  136. return (self.args, self.kwargs,
  137. dict(self.options, **options) if options else self.options)
  138. return (tuple(args) + tuple(self.args) if args else self.args,
  139. dict(self.kwargs, **kwargs) if kwargs else self.kwargs,
  140. dict(self.options, **options) if options else self.options)
  141. def clone(self, args=(), kwargs={}, app=None, **opts):
  142. # need to deepcopy options so origins links etc. is not modified.
  143. if args or kwargs or opts:
  144. args, kwargs, opts = self._merge(args, kwargs, opts)
  145. else:
  146. args, kwargs, opts = self.args, self.kwargs, self.options
  147. s = Signature.from_dict({'task': self.task, 'args': tuple(args),
  148. 'kwargs': kwargs, 'options': deepcopy(opts),
  149. 'subtask_type': self.subtask_type,
  150. 'immutable': self.immutable},
  151. app=app or self._app)
  152. s._type = self._type
  153. return s
  154. partial = clone
  155. def freeze(self, _id=None, group_id=None, chord=None):
  156. opts = self.options
  157. try:
  158. tid = opts['task_id']
  159. except KeyError:
  160. tid = opts['task_id'] = _id or uuid()
  161. if 'reply_to' not in opts:
  162. opts['reply_to'] = self.app.oid
  163. if group_id:
  164. opts['group_id'] = group_id
  165. if chord:
  166. opts['chord'] = chord
  167. return self.app.AsyncResult(tid)
  168. _freeze = freeze
  169. def replace(self, args=None, kwargs=None, options=None):
  170. s = self.clone()
  171. if args is not None:
  172. s.args = args
  173. if kwargs is not None:
  174. s.kwargs = kwargs
  175. if options is not None:
  176. s.options = options
  177. return s
  178. def set(self, immutable=None, **options):
  179. if immutable is not None:
  180. self.set_immutable(immutable)
  181. self.options.update(options)
  182. return self
  183. def set_immutable(self, immutable):
  184. self.immutable = immutable
  185. def apply_async(self, args=(), kwargs={}, **options):
  186. try:
  187. _apply = self._apply_async
  188. except IndexError: # no tasks for chain, etc to find type
  189. return
  190. # For callbacks: extra args are prepended to the stored args.
  191. if args or kwargs or options:
  192. args, kwargs, options = self._merge(args, kwargs, options)
  193. else:
  194. args, kwargs, options = self.args, self.kwargs, self.options
  195. return _apply(args, kwargs, **options)
  196. def append_to_list_option(self, key, value):
  197. items = self.options.setdefault(key, [])
  198. if not isinstance(items, MutableSequence):
  199. items = self.options[key] = [items]
  200. if value not in items:
  201. items.append(value)
  202. return value
  203. def link(self, callback):
  204. return self.append_to_list_option('link', callback)
  205. def link_error(self, errback):
  206. return self.append_to_list_option('link_error', errback)
  207. def flatten_links(self):
  208. return list(_chain.from_iterable(_chain(
  209. [[self]],
  210. (link.flatten_links()
  211. for link in maybe_list(self.options.get('link')) or [])
  212. )))
  213. def __or__(self, other):
  214. if isinstance(other, group):
  215. other = maybe_unroll_group(other)
  216. if not isinstance(self, chain) and isinstance(other, chain):
  217. return chain((self, ) + other.tasks, app=self._app)
  218. elif isinstance(other, chain):
  219. return chain(*self.tasks + other.tasks, app=self._app)
  220. elif isinstance(other, Signature):
  221. if isinstance(self, chain):
  222. return chain(*self.tasks + (other, ), app=self._app)
  223. return chain(self, other, app=self._app)
  224. return NotImplemented
  225. def __deepcopy__(self, memo):
  226. memo[id(self)] = self
  227. return dict(self)
  228. def __invert__(self):
  229. return self.apply_async().get()
  230. def __reduce__(self):
  231. # for serialization, the task type is lazily loaded,
  232. # and not stored in the dict itself.
  233. return subtask, (dict(self), )
  234. def reprcall(self, *args, **kwargs):
  235. args, kwargs, _ = self._merge(args, kwargs, {})
  236. return reprcall(self['task'], args, kwargs)
  237. def election(self):
  238. type = self.type
  239. app = type.app
  240. tid = self.options.get('task_id') or uuid()
  241. with app.producer_or_acquire(None) as P:
  242. props = type.backend.on_task_call(P, tid)
  243. app.control.election(tid, 'task', self.clone(task_id=tid, **props),
  244. connection=P.connection)
  245. return type.AsyncResult(tid)
  246. def __repr__(self):
  247. return self.reprcall()
  248. @cached_property
  249. def type(self):
  250. return self._type or self.app.tasks[self['task']]
  251. @cached_property
  252. def app(self):
  253. return self._app or current_app
  254. @cached_property
  255. def AsyncResult(self):
  256. try:
  257. return self.type.AsyncResult
  258. except KeyError: # task not registered
  259. return self.app.AsyncResult
  260. @cached_property
  261. def _apply_async(self):
  262. try:
  263. return self.type.apply_async
  264. except KeyError:
  265. return _partial(self.app.send_task, self['task'])
  266. id = _getitem_property('options.task_id')
  267. task = _getitem_property('task')
  268. args = _getitem_property('args')
  269. kwargs = _getitem_property('kwargs')
  270. options = _getitem_property('options')
  271. subtask_type = _getitem_property('subtask_type')
  272. immutable = _getitem_property('immutable')
  273. @Signature.register_type
  274. class chain(Signature):
  275. def __init__(self, *tasks, **options):
  276. tasks = (regen(tasks[0]) if len(tasks) == 1 and is_list(tasks[0])
  277. else tasks)
  278. Signature.__init__(
  279. self, 'celery.chain', (), {'tasks': tasks}, **options
  280. )
  281. self.tasks = tasks
  282. self.subtask_type = 'chain'
  283. def __call__(self, *args, **kwargs):
  284. if self.tasks:
  285. return self.apply_async(args, kwargs)
  286. @classmethod
  287. def from_dict(self, d, app=None):
  288. tasks = [maybe_signature(t, app=app) for t in d['kwargs']['tasks']]
  289. if d['args'] and tasks:
  290. # partial args passed on to first task in chain (Issue #1057).
  291. tasks[0]['args'] = tasks[0]._merge(d['args'])[0]
  292. return chain(*tasks, app=app, **kwdict(d['options']))
  293. @property
  294. def type(self):
  295. try:
  296. return self._type or self.tasks[0].type.app.tasks['celery.chain']
  297. except KeyError:
  298. return self.app.tasks['celery.chain']
  299. def __repr__(self):
  300. return ' | '.join(repr(t) for t in self.tasks)
  301. class _basemap(Signature):
  302. _task_name = None
  303. _unpack_args = itemgetter('task', 'it')
  304. def __init__(self, task, it, **options):
  305. Signature.__init__(
  306. self, self._task_name, (),
  307. {'task': task, 'it': regen(it)}, immutable=True, **options
  308. )
  309. def apply_async(self, args=(), kwargs={}, **opts):
  310. # need to evaluate generators
  311. task, it = self._unpack_args(self.kwargs)
  312. return self.type.apply_async(
  313. (), {'task': task, 'it': list(it)}, **opts
  314. )
  315. @classmethod
  316. def from_dict(cls, d, app=None):
  317. return cls(*cls._unpack_args(d['kwargs']), app=app, **d['options'])
  318. @Signature.register_type
  319. class xmap(_basemap):
  320. _task_name = 'celery.map'
  321. def __repr__(self):
  322. task, it = self._unpack_args(self.kwargs)
  323. return '[{0}(x) for x in {1}]'.format(task.task,
  324. truncate(repr(it), 100))
  325. @Signature.register_type
  326. class xstarmap(_basemap):
  327. _task_name = 'celery.starmap'
  328. def __repr__(self):
  329. task, it = self._unpack_args(self.kwargs)
  330. return '[{0}(*x) for x in {1}]'.format(task.task,
  331. truncate(repr(it), 100))
  332. @Signature.register_type
  333. class chunks(Signature):
  334. _unpack_args = itemgetter('task', 'it', 'n')
  335. def __init__(self, task, it, n, **options):
  336. Signature.__init__(
  337. self, 'celery.chunks', (),
  338. {'task': task, 'it': regen(it), 'n': n},
  339. immutable=True, **options
  340. )
  341. @classmethod
  342. def from_dict(self, d, app=None):
  343. return chunks(*self._unpack_args(d['kwargs']), app=app, **d['options'])
  344. def apply_async(self, args=(), kwargs={}, **opts):
  345. return self.group().apply_async(args, kwargs, **opts)
  346. def __call__(self, **options):
  347. return self.group()(**options)
  348. def group(self):
  349. # need to evaluate generators
  350. task, it, n = self._unpack_args(self.kwargs)
  351. return group((xstarmap(task, part, app=self._app)
  352. for part in _chunks(iter(it), n)),
  353. app=self._app)
  354. @classmethod
  355. def apply_chunks(cls, task, it, n, app=None):
  356. return cls(task, it, n, app=app)()
  357. def _maybe_group(tasks):
  358. if isinstance(tasks, group):
  359. tasks = list(tasks.tasks)
  360. elif isinstance(tasks, Signature):
  361. tasks = [tasks]
  362. else:
  363. tasks = regen(tasks)
  364. return tasks
  365. def _maybe_clone(tasks, app):
  366. return [s.clone() if isinstance(s, Signature) else signature(s, app=app)
  367. for s in tasks]
  368. @Signature.register_type
  369. class group(Signature):
  370. def __init__(self, *tasks, **options):
  371. if len(tasks) == 1:
  372. tasks = _maybe_group(tasks[0])
  373. Signature.__init__(
  374. self, 'celery.group', (), {'tasks': tasks}, **options
  375. )
  376. self.tasks, self.subtask_type = tasks, 'group'
  377. @classmethod
  378. def from_dict(self, d, app=None):
  379. tasks = [maybe_signature(t, app=app) for t in d['kwargs']['tasks']]
  380. if d['args'] and tasks:
  381. # partial args passed on to all tasks in the group (Issue #1057).
  382. for task in tasks:
  383. task['args'] = task._merge(d['args'])[0]
  384. return group(tasks, app=app, **kwdict(d['options']))
  385. def apply_async(self, args=(), kwargs=None, add_to_parent=True, **options):
  386. tasks = _maybe_clone(self.tasks, app=self._app)
  387. if not tasks:
  388. return self.freeze()
  389. type = self.type
  390. return type(*type.prepare(dict(self.options, **options), tasks, args),
  391. add_to_parent=add_to_parent)
  392. def set_immutable(self, immutable):
  393. for task in self.tasks:
  394. task.set_immutable(immutable)
  395. def link(self, sig):
  396. # Simply link to first task
  397. sig = sig.clone().set(immutable=True)
  398. return self.tasks[0].link(sig)
  399. def link_error(self, sig):
  400. sig = sig.clone().set(immutable=True)
  401. return self.tasks[0].link_error(sig)
  402. def apply(self, *args, **kwargs):
  403. if not self.tasks:
  404. return self.freeze() # empty group returns GroupResult
  405. return Signature.apply(self, *args, **kwargs)
  406. def __call__(self, *partial_args, **options):
  407. return self.apply_async(partial_args, **options)
  408. def freeze(self, _id=None, group_id=None, chord=None):
  409. opts = self.options
  410. try:
  411. gid = opts['task_id']
  412. except KeyError:
  413. gid = opts['task_id'] = uuid()
  414. if group_id:
  415. opts['group_id'] = group_id
  416. if chord:
  417. opts['chord'] = group_id
  418. new_tasks, results = [], []
  419. for task in self.tasks:
  420. task = maybe_signature(task, app=self._app).clone()
  421. results.append(task.freeze(group_id=group_id, chord=chord))
  422. new_tasks.append(task)
  423. self.tasks = self.kwargs['tasks'] = new_tasks
  424. return self.app.GroupResult(gid, results)
  425. _freeze = freeze
  426. def skew(self, start=1.0, stop=None, step=1.0):
  427. it = fxrange(start, stop, step, repeatlast=True)
  428. for task in self.tasks:
  429. task.set(countdown=next(it))
  430. return self
  431. def __iter__(self):
  432. return iter(self.tasks)
  433. def __repr__(self):
  434. return repr(self.tasks)
  435. @property
  436. def app(self):
  437. return self._app or (self.tasks[0].app if self.tasks else current_app)
  438. @property
  439. def type(self):
  440. if self._type:
  441. return self._type
  442. # taking the app from the first task in the list, there may be a
  443. # better solution for this, e.g. to consolidate tasks with the same
  444. # app and apply them in batches.
  445. return self.app.tasks[self['task']]
  446. @Signature.register_type
  447. class chord(Signature):
  448. def __init__(self, header, body=None, task='celery.chord',
  449. args=(), kwargs={}, **options):
  450. Signature.__init__(
  451. self, task, args,
  452. dict(kwargs, header=_maybe_group(header),
  453. body=maybe_signature(body, app=self._app)), **options
  454. )
  455. self.subtask_type = 'chord'
  456. def freeze(self, _id=None, group_id=None, chord=None):
  457. return self.body.freeze(_id, group_id=group_id, chord=chord)
  458. @classmethod
  459. def from_dict(self, d, app=None):
  460. args, d['kwargs'] = self._unpack_args(**kwdict(d['kwargs']))
  461. return self(*args, app=app, **kwdict(d))
  462. @staticmethod
  463. def _unpack_args(header=None, body=None, **kwargs):
  464. # Python signatures are better at extracting keys from dicts
  465. # than manually popping things off.
  466. return (header, body), kwargs
  467. @property
  468. def app(self):
  469. # we will be able to fix this mess in 3.2 when we no longer
  470. # require an actual task implementation for chord/group
  471. if self._app:
  472. return self._app
  473. app = None if self.body is None else self.body.app
  474. if app is None:
  475. try:
  476. app = self.tasks[0].app
  477. except IndexError:
  478. app = None
  479. return app if app is not None else current_app
  480. @property
  481. def type(self):
  482. if self._type:
  483. return self._type
  484. return self.app.tasks['celery.chord']
  485. def apply_async(self, args=(), kwargs={}, task_id=None,
  486. producer=None, publisher=None, connection=None,
  487. router=None, result_cls=None, **options):
  488. args = tuple(args) + tuple(self.args) if args else self.args
  489. body = kwargs.get('body') or self.kwargs['body']
  490. kwargs = dict(self.kwargs, **kwargs)
  491. body = body.clone(**options)
  492. _chord = self.type
  493. if _chord.app.conf.CELERY_ALWAYS_EAGER:
  494. return self.apply((), kwargs, task_id=task_id, **options)
  495. res = body.freeze(task_id)
  496. parent = _chord(self.tasks, body, args, **options)
  497. res.parent = parent
  498. return res
  499. def __call__(self, body=None, **options):
  500. return self.apply_async((), {'body': body} if body else {}, **options)
  501. def clone(self, *args, **kwargs):
  502. s = Signature.clone(self, *args, **kwargs)
  503. # need to make copy of body
  504. try:
  505. s.kwargs['body'] = s.kwargs['body'].clone()
  506. except (AttributeError, KeyError):
  507. pass
  508. return s
  509. def link(self, callback):
  510. self.body.link(callback)
  511. return callback
  512. def link_error(self, errback):
  513. self.body.link_error(errback)
  514. return errback
  515. def set_immutable(self, immutable):
  516. # changes mutability of header only, not callback.
  517. for task in self.tasks:
  518. task.set_immutable(immutable)
  519. def __repr__(self):
  520. if self.body:
  521. return self.body.reprcall(self.tasks)
  522. return '<chord without body: {0.tasks!r}>'.format(self)
  523. tasks = _getitem_property('kwargs.header')
  524. body = _getitem_property('kwargs.body')
  525. def signature(varies, args=(), kwargs={}, options={}, app=None, **kw):
  526. if isinstance(varies, dict):
  527. if isinstance(varies, Signature):
  528. return varies.clone(app=app)
  529. return Signature.from_dict(varies, app=app)
  530. return Signature(varies, args, kwargs, options, app=app, **kw)
  531. subtask = signature # XXX compat
  532. def maybe_signature(d, app=None):
  533. if d is not None:
  534. if isinstance(d, dict):
  535. if not isinstance(d, Signature):
  536. return signature(d, app=app)
  537. elif isinstance(d, list):
  538. return [maybe_signature(s, app=app) for s in d]
  539. if app is not None:
  540. d._app = app
  541. return d
  542. maybe_subtask = maybe_signature # XXX compat