canvas.py 19 KB

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