tasks.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import, unicode_literals
  3. from time import sleep
  4. from celery import shared_task
  5. from celery.utils.log import get_task_logger
  6. logger = get_task_logger(__name__)
  7. @shared_task
  8. def add(x, y):
  9. """Add two numbers."""
  10. return x + y
  11. @shared_task
  12. def print_unicode(log_message='hå它 valmuefrø', print_message='hiöäüß'):
  13. """Task that both logs and print strings containing funny characters."""
  14. logger.warning(log_message)
  15. print(print_message)
  16. @shared_task
  17. def sleeping(i, **_):
  18. """Task sleeping for ``i`` seconds, and returning nothing."""
  19. sleep(i)
  20. @shared_task(bind=True)
  21. def ids(self, i):
  22. """Returns a tuple of ``root_id``, ``parent_id`` and
  23. the argument passed as ``i``."""
  24. return self.request.root_id, self.request.parent_id, i
  25. @shared_task(bind=True)
  26. def collect_ids(self, res, i):
  27. """Used as a callback in a chain or group where the previous tasks
  28. are :task:`ids`: returns a tuple of::
  29. (previous_result, (root_id, parent_id, i))
  30. """
  31. return res, (self.request.root_id, self.request.parent_id, i)