123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- # -*- coding: utf-8 -*-
- from __future__ import absolute_import, unicode_literals
- from time import sleep
- from celery import shared_task, group
- from celery.utils.log import get_task_logger
- logger = get_task_logger(__name__)
- @shared_task
- def add(x, y):
- """Add two numbers."""
- return x + y
- @shared_task(bind=True)
- def add_replaced(self, x, y):
- """Add two numbers (via the add task)."""
- raise self.replace(add.s(x, y))
- @shared_task(bind=True)
- def add_to_all(self, nums, val):
- """Add the given value to all supplied numbers."""
- subtasks = [add.s(num, val) for num in nums]
- raise self.replace(group(*subtasks))
- @shared_task
- def print_unicode(log_message='hå它 valmuefrø', print_message='hiöäüß'):
- """Task that both logs and print strings containing funny characters."""
- logger.warning(log_message)
- print(print_message)
- @shared_task
- def sleeping(i, **_):
- """Task sleeping for ``i`` seconds, and returning nothing."""
- sleep(i)
- @shared_task(bind=True)
- def ids(self, i):
- """Returns a tuple of ``root_id``, ``parent_id`` and
- the argument passed as ``i``."""
- return self.request.root_id, self.request.parent_id, i
- @shared_task(bind=True)
- def collect_ids(self, res, i):
- """Used as a callback in a chain or group where the previous tasks
- are :task:`ids`: returns a tuple of::
- (previous_result, (root_id, parent_id, i))
- """
- return res, (self.request.root_id, self.request.parent_id, i)
|