tasks.rst 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. =======
  2. Tasks
  3. =======
  4. .. module:: celery.task.base
  5. A task is a class that encapsulates a function and its execution options.
  6. With a function ``create_user``, that takes two arguments: ``username`` and
  7. ``password``, you can create a task like this:
  8. .. code-block:: python
  9. from celery.task import Task
  10. class CreateUserTask(Task):
  11. def run(self, username, password):
  12. create_user(username, password)
  13. For convenience there is a shortcut decorator that turns any function into
  14. a task, ``celery.decorators.task``:
  15. .. code-block:: python
  16. from celery.decorators import task
  17. from django.contrib.auth import User
  18. @task()
  19. def create_user(username, password):
  20. User.objects.create(username=username, password=password)
  21. Note the parens after ``@task()`` the task decorator takes any execution
  22. options the ``Task`` class does:
  23. .. code-block:: python
  24. @task(serializer="json")
  25. def create_user(username, password):
  26. User.objects.create(username=username, password=password)
  27. An alternative way to use the decorator is to give the function as an argument
  28. instead, but if you do this be sure to set the resulting tasks ``__name__``
  29. attribute, so pickle is able to find it in reverse:
  30. .. code-block:: python
  31. create_user_task = task()(create_user)
  32. create_user_task.__name__ = "create_user_task"
  33. Default keyword arguments
  34. =========================
  35. Celery supports a set of default arguments that can be forwarded to any task.
  36. task can choose not to take these, or only list the ones it want
  37. (the worker will do the right thing).
  38. The current default keyword arguments are:
  39. * logfile
  40. The currently used log file, can be passed on to ``self.get_logger``
  41. to gain access to the workers log file. See `Logging`_.
  42. * loglevel
  43. The current loglevel used.
  44. * task_id
  45. The unique id of the executing task.
  46. * task_name
  47. Name of the executing task.
  48. * task_retries
  49. How many times the current task has been retried.
  50. (an integer starting a ``0``).
  51. Logging
  52. =======
  53. You can use the workers logger to add some diagnostic output to
  54. the worker log:
  55. .. code-block:: python
  56. class AddTask(Task):
  57. def run(self, x, y, **kwargs):
  58. logger = self.get_logger(**kwargs)
  59. logger.info("Adding %s + %s" % (x, y))
  60. return x + y
  61. or using the decorator syntax:
  62. .. code-block:: python
  63. @task()
  64. def add(x, y, **kwargs):
  65. logger = add.get_logger(**kwargs)
  66. logger.info("Adding %s + %s" % (x, y))
  67. return x + y
  68. There are several logging levels available, and the workers ``loglevel``
  69. setting decides whether they will be sent to the log file or not.
  70. Retrying a task if something fails
  71. ==================================
  72. Simply use :meth:`Task.retry` to re-sent the task, it will
  73. do the right thing, and respect the :attr:`Task.max_retries`
  74. attribute:
  75. .. code-block:: python
  76. @task()
  77. def send_twitter_status(oauth, tweet, **kwargs):
  78. try:
  79. twitter = Twitter(oauth)
  80. twitter.update_status(tweet)
  81. except (Twitter.FailWhaleError, Twitter.LoginError), exc:
  82. send_twitter_status.retry(args=[oauth, tweet], kwargs, exc=exc)
  83. Here we used the ``exc`` argument to pass the current exception to
  84. :meth:`Task.retry`. At each step of the retry this exception
  85. is available as the tombstone (result) of the task, when
  86. :attr:`Task.max_retries` has been exceeded this is the exception
  87. raised. However, if an ``exc`` argument is not provided the
  88. :exc:`RetryTaskError` exception is raised instead.
  89. Using a custom retry delay
  90. --------------------------
  91. The default countdown is in the tasks
  92. :attr:`Task.default_retry_delay` attribute, which by
  93. default is set to 3 minutes.
  94. You can also provide the ``countdown`` argument to
  95. :meth:`Task.retry` to override this default.
  96. .. code-block:: python
  97. class MyTask(Task):
  98. default_retry_delay = 30 * 60 # retry in 30 minutes
  99. def run(self, x, y, **kwargs):
  100. try:
  101. ...
  102. except Exception, exc:
  103. self.retry([x, y], kwargs, exc=exc,
  104. countdown=60) # override the default and
  105. # - retry in 1 minute
  106. Task options
  107. ============
  108. * name
  109. This is the name the task is registered as.
  110. You can set this name manually, or just use the default which is
  111. atomatically generated using the module and class name.
  112. * abstract
  113. Abstract classes are not registered, so they're
  114. only used for making new task types by subclassing.
  115. * max_retries
  116. The maximum number of attempted retries before giving up.
  117. If this is exceeded the :exc`celery.execptions.MaxRetriesExceeded`
  118. exception will be raised. Note that you have to retry manually, it's
  119. not something that happens automatically.
  120. * default_retry_delay
  121. Default time in seconds before a retry of the task should be
  122. executed. Default is a 1 minute delay.
  123. * rate_limit
  124. Set the rate limit for this task type,
  125. if this is ``None`` no rate limit is in effect.
  126. The rate limits can be specified in seconds, minutes or hours
  127. by appending ``"/s"``, ``"/m"`` or "``/h"``". If this is an integer
  128. it is interpreted as seconds. Example: ``"100/m" (hundred tasks a
  129. minute). Default is the ``CELERY_DEFAULT_RATE_LIMIT`` setting (which
  130. is off if not specified).
  131. * ignore_result
  132. Don't store the status and return value. This means you can't
  133. use the :class:`celery.result.AsyncResult` to check if the task is
  134. done, or get its return value. Only use if you need the performance
  135. and is able live without these features. Any exceptions raised will
  136. store the return value/status as usual.
  137. * disable_error_emails
  138. Disable all error e-mails for this task.
  139. * serializer
  140. A string identifying the default serialization
  141. method to use. Defaults to the ``CELERY_TASK_SERIALIZER`` setting.
  142. Can be ``pickle`` ``json``, ``yaml``, or any custom serialization
  143. methods that have been registered with
  144. :mod:`carrot.serialization.registry`.
  145. Please see :doc:`executing` for more information.
  146. Message and routing options
  147. ---------------------------
  148. * routing_key
  149. Override the global default ``routing_key`` for this task.
  150. * exchange
  151. Override the global default ``exchange`` for this task.
  152. * mandatory
  153. If set, the task message has mandatory routing. By default the task
  154. is silently dropped by the broker if it can't be routed to a queue.
  155. However - If the task is mandatory, an exception will be raised
  156. instead.
  157. * immediate
  158. Request immediate delivery. If the task cannot be routed to a
  159. task worker immediately, an exception will be raised. This is
  160. instead of the default behaviour, where the broker will accept and
  161. queue the task, but with no guarantee that the task will ever
  162. be executed.
  163. * priority
  164. The message priority. A number from ``0`` to ``9``, where ``0`` is the
  165. highest. Note that RabbitMQ doesn't support priorities yet.
  166. Please see :doc:`executing` for descriptions of these options.
  167. How it works
  168. ============
  169. Here comes the technical details, this part isn't something you need to know,
  170. but you may be interested, so here goes.
  171. All defined tasks are listed in a registry. The registry contains
  172. a list of task names and their task classes. You can investigate this registry
  173. by yourself:
  174. .. code-block:: python
  175. >>> from celery.task import registry
  176. >>> from celery import task
  177. >>> registry.tasks
  178. {'celery.delete_expired_task_meta':
  179. <celery.task.builtins.DeleteExpiredTaskMetaTask object at 0x101d1f510>,
  180. 'celery.execute_remote':
  181. <celery.task.base.ExecuteRemoteTask object at 0x101d17890>,
  182. 'celery.task.rest.RESTProxyTask':
  183. <celery.task.rest.RESTProxyTask object at 0x101d1f410>,
  184. 'celery.task.rest.Task': <celery.task.rest.Task object at 0x101d1f4d0>,
  185. 'celery.map_async':
  186. <celery.task.base.AsynchronousMapTask object at 0x101d17910>,
  187. 'celery.ping': <celery.task.builtins.PingTask object at 0x101d1f550>}
  188. This is the list of tasks built-in to celery. Note that we had to import
  189. ``celery.task`` first for these to show up. This is because the tasks will
  190. only be registered when the module it is defined in is imported.
  191. When using the default loader the loader imports any modules listed in the
  192. ``CELERY_IMPORTS`` setting. If using Django it loads all ``tasks.py`` modules
  193. for the applications listed in ``INSTALLED_APPS``. If you want to do something
  194. special you can create your own loader to do what you want.
  195. The entity responsible for registering your task in the registry is a
  196. metaclass, :class:`TaskType`, this is the default metaclass for
  197. ``Task``. If you want to register your task manually you can set the
  198. ``abstract`` attribute:
  199. .. code-block:: python
  200. class MyTask(Task):
  201. abstract = True
  202. This way the task won't be registered, but any task subclassing it will.
  203. So when we send a task, we don't send the function code, we just send the name
  204. of the task, so when the worker receives the message it can just look it up in
  205. the task registry to find the execution code.
  206. This means that your workers must optimally be updated with the same software
  207. as the client, this is a drawback, but the alternative is a technical
  208. challenge that has yet to be solved.