Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

methods.py 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # -*- coding: utf-8 -*-
  2. """
  3. celery.contrib.methods
  4. ======================
  5. Task decorator that supports creating tasks out of methods.
  6. Examples
  7. --------
  8. .. code-block:: python
  9. from celery.contrib.methods import task
  10. class X(object):
  11. @task()
  12. def add(self, x, y):
  13. return x + y
  14. or with any task decorator:
  15. .. code-block:: python
  16. from celery.contrib.methods import task_method
  17. class X(object):
  18. @app.task(filter=task_method)
  19. def add(self, x, y):
  20. return x + y
  21. .. note::
  22. The task must use the new Task base class (:class:`celery.Task`),
  23. and the old base class using classmethods (``celery.task.Task``,
  24. ``celery.task.base.Task``).
  25. This means that you have to use the task decorator from a Celery app
  26. instance, and not the old-API:
  27. .. code-block:: python
  28. from celery import task # BAD
  29. from celery.task import task # ALSO BAD
  30. # GOOD:
  31. app = Celery(...)
  32. @app.task(filter=task_method)
  33. def foo(self): pass
  34. # ALSO GOOD:
  35. from celery import current_app
  36. @current_app.task(filter=task_method)
  37. def foo(self): pass
  38. # ALSO GOOD:
  39. from celery import shared_task
  40. @shared_task(filter=task_method)
  41. def foo(self): pass
  42. Caveats
  43. -------
  44. - Automatic naming won't be able to know what the class name is.
  45. The name will still be module_name + task_name,
  46. so two methods with the same name in the same module will collide
  47. so that only one task can run:
  48. .. code-block:: python
  49. class A(object):
  50. @task()
  51. def add(self, x, y):
  52. return x + y
  53. class B(object):
  54. @task()
  55. def add(self, x, y):
  56. return x + y
  57. would have to be written as:
  58. .. code-block:: python
  59. class A(object):
  60. @task(name='A.add')
  61. def add(self, x, y):
  62. return x + y
  63. class B(object):
  64. @task(name='B.add')
  65. def add(self, x, y):
  66. return x + y
  67. """
  68. from __future__ import absolute_import
  69. from celery import current_app
  70. __all__ = ['task_method', 'task']
  71. class task_method(object):
  72. def __init__(self, task, *args, **kwargs):
  73. self.task = task
  74. def __get__(self, obj, type=None):
  75. if obj is None:
  76. return self.task
  77. task = self.task.__class__()
  78. task.__self__ = obj
  79. return task
  80. def task(*args, **kwargs):
  81. return current_app.task(*args, **dict(kwargs, filter=task_method))