tasks.py 1.0 KB

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