executing.rst 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. =================
  2. Executing Tasks
  3. =================
  4. Executing tasks is done with ``apply_async``, and it's shortcut ``delay``.
  5. ``delay`` is simple and convenient, as it looks like calling a regular
  6. function:
  7. .. code-block:: python
  8. MyTask.delay(arg1, arg2, kwarg1="x", kwarg2="y")
  9. The same thing using ``apply_async`` is written like this:
  10. .. code-block:: python
  11. AddTask.delay(args=[arg1, arg2], kwargs={"kwarg1": "x", "kwarg2": "y"})
  12. But ``delay`` doesn't give you as much control as using ``apply_async``.
  13. With ``apply_async`` you can override the execution options available as attributes on
  14. the ``Task`` class; ``routing_key``, ``exchange``, ``immediate``, ``mandatory``,
  15. ``priority``, and ``serializer``. In addition you can set a countdown or an eta, provide
  16. a custom broker connection or change the broker connection timeout.
  17. Let's go over these in more detail. The following examples uses this simple
  18. task, used to add two numbers:
  19. .. code-block:: python
  20. class AddTask(Task):
  21. def run(self, x, y):
  22. return x + y
  23. ETA and countdown
  24. -----------------
  25. The ETA (estimated time of arrival) lets you set a specific date and time for
  26. when after, your task should execute. ``countdown`` is a shortcut to set this
  27. by seconds into the future.
  28. .. code-block:: python
  29. >>> result = AddTask.apply_async(args=[10, 10], countdown=3)
  30. >>> result.get() # this takes at least 3 seconds to return
  31. 20
  32. Note that your task is guaranteed to be executed at some time *after* the
  33. specified date and time has passed, but not necessarily at that exact time.
  34. While ``countdown`` is an integer, ``eta`` must be a ``datetime`` object,
  35. specifying an exact date and time in the future. This is good if you already
  36. have a ``datatime`` object and need to modify it with a ``timedelta``, or when
  37. using time in seconds is not very readable.
  38. .. code-block:: python
  39. from datetime import datetime, timedelta
  40. def quickban(username):
  41. """Ban user for 24 hours."""
  42. ban(username)
  43. tomorrow = datetime.now() + timedelta(days=1)
  44. UnbanTask.apply_async(args=[username], eta=tomorrow)
  45. Serializer
  46. ----------
  47. The default serializer used is :mod:`pickle`, but you can change this default by
  48. changing the ``CELERY_SERIALIZER`` configuration directive. There is built-in
  49. support for using ``pickle``, ``JSON`` and ``YAML``, and you can add your own
  50. custom serializers by registering them in the carrot serializer registry.
  51. You don't have to do any work on the worker receiving the task, the
  52. serialization method is sent with the message so the worker knows how to
  53. deserialize any task (of course, if you use a custom serializer, this must also be
  54. registered in the worker.)
  55. When sending a task the serializition method is taken from the following
  56. places in order: The ``serializer`` argument to ``apply_async``, the
  57. Task's ``serializer`` attribute, and finally the global default ``CELERY_SERIALIZER``
  58. configuration directive.
  59. .. code-block:: python
  60. >>> AddTask.apply_async(args=[10, 10], serializer="JSON")
  61. Connections and connection timeouts.
  62. ------------------------------------
  63. Currently there is no support for broker connection pooling in celery, but
  64. this might change in the future. This is something you need to be aware of
  65. when sending more than one task at a time, as ``apply_async`` establishes and
  66. closes a connection every time.
  67. If you need to send more than one task at the same time, it's a good idea to
  68. establish the connection yourself and pass it to ``apply_async``:
  69. .. code-block:: python
  70. from celery.messaging import establish_connection
  71. numbers = [(2, 2), (4, 4), (8, 8), (16, 16)]
  72. results = []
  73. connection = establish_connection()
  74. try:
  75. for args in numbers:
  76. res = AddTask.apply_async(args=args, connection=connection)
  77. results.append(res)
  78. finally:
  79. connection.close()
  80. print([res.get() for res in results])
  81. In Python 2.5 and above, you can use the ``with`` statement:
  82. .. code-block:: python
  83. from __future__ import with_statement
  84. from celery.messaging import establish_connection
  85. numbers = [(2, 2), (4, 4), (8, 8), (16, 16)]
  86. results = []
  87. with establish_connection() as connection:
  88. for args in numbers:
  89. res = AddTask.apply_async(args=args, connection=connection)
  90. results.append(res)
  91. print([res.get() for res in results])
  92. *NOTE* TaskSets already re-uses the same connection, but not if you need to
  93. execute more than one TaskSet.
  94. The connection timeout is the number of seconds to wait before we give up on
  95. establishing the connection, you can set this with the ``connect_timeout``
  96. argument to ``apply_async``:
  97. .. code-block:: python
  98. AddTask.apply_async([10, 10], connect_timeout=3)
  99. or if you handle the connection manually:
  100. .. code-block:: python
  101. connection = establish_connection(connect_timeout=3)
  102. Routing options
  103. ---------------
  104. Celery uses the AMQP routing mechanisms to route tasks to different workers.
  105. You can route tasks using the following entitites: exchange, queue and routing key.
  106. Messages (tasks) are sent to exchanges, a queue binds to an exchange with a
  107. routing key. Let's look at an example:
  108. Our application has a lot of tasks, some process video, others process images,
  109. and some gathers collective intelligence about users. Some of these have
  110. higher priority than others so we want to make sure the high priority tasks
  111. get sent to powerful machines, while low priority tasks are sent to dedicated
  112. machines that can handle these at their own pace, uninterrupted.
  113. For the sake of example we have only one exchange called ``tasks``.
  114. There are different types of exchanges that matches the routing key in
  115. different ways, the exchange types are:
  116. * direct
  117. Matches the routing key exactly.
  118. * topic
  119. In the topic exchange the routing key is made up of words separated by dots (``.``).
  120. Words can be matched by the wildcars ``*`` and ``#``, where ``*`` matches one
  121. exact word, and ``#`` matches one or many.
  122. For example, ``*.stock.#`` matches the routing keys ``usd.stock`` and
  123. ``euro.stock.db`` but not ``stock.nasdaq``.
  124. (there are also other exchange types, but these are not used by celery)
  125. So, we create three queues, ``video``, ``image`` and ``lowpri`` that binds to
  126. our ``tasks`` exchange, for the queues we use the following binding keys::
  127. video: video.#
  128. image: image.#
  129. lowpri: misc.#
  130. Now we can send our tasks to different worker machines, by making the workers
  131. listen to different queues:
  132. .. code-block:: python
  133. >>> CompressVideoTask.apply_async(args=[filename],
  134. ... routing_key="video.compress")
  135. >>> ImageRotateTask.apply_async(args=[filename, 360],
  136. routing_key="image.rotate")
  137. >>> ImageCropTask.apply_async(args=[filename, selection],
  138. routing_key="image.crop")
  139. >>> UpdateReccomendationsTask.apply_async(routing_key="misc.recommend")
  140. Later, if suddenly the image crop task is consuming a lot of resources,
  141. we can bind some new workers to handle just the ``"image.crop"`` task,
  142. by creating a new queue that binds to ``"image.crop``".
  143. AMQP options
  144. ------------
  145. * mandatory
  146. This sets the delivery to be mandatory. An exception will be raised
  147. if there are no running workers able to take on the task.
  148. * immediate
  149. Request immediate delivery. Will raise an exception
  150. if the task cannot be routed to a worker immediately.
  151. * priority
  152. A number between ``0`` and ``9``, where ``0`` is the highest priority.
  153. Note that RabbitMQ does not implement AMQP priorities, and maybe your broker
  154. does not either, please consult your brokers documentation for more
  155. information.