workers.rst 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. ===============
  2. Workers Guide
  3. ===============
  4. Starting the worker
  5. ===================
  6. Starting celeryd in the foreground::
  7. $ celeryd --loglevel=INFO
  8. You probably want to use a daemonization tool to start and stop
  9. ``celeryd`` in the background, see :doc:`../cookbook/daemonizing` for help using
  10. some of the most popular solutions.
  11. For a full list of available command line options see :mod:`~celery.bin.celeryd`.
  12. You can also start multiple celeryd's on the same machine, but if you do so
  13. be sure to give a unique name to each individual worker by specifying the
  14. ``-hostname`` argument::
  15. $ celeryd --loglevel=INFO --concurrency=10 -n worker1.example.com
  16. $ celeryd --loglevel=INFO --concurrency=10 -n worker2.example.com
  17. $ celeryd --loglevel=INFO --concurrency=10 -n worker3.example.com
  18. Stopping the worker
  19. ===================
  20. Shutdown should be accomplished using the ``TERM`` signal (although ``INT``
  21. also works).
  22. When shutdown is initiated the worker will finish any tasks it's currently
  23. executing before it terminates, so if these tasks are important you should
  24. wait for it to finish before doing anything drastic (like sending the ``KILL``
  25. signal).
  26. If the worker won't shutdown after considerate time, you probably have hanging
  27. tasks, in this case it's safe to use the ``KILL`` signal but be aware that
  28. currently executing tasks will be lost (unless the tasks have the
  29. :attr:`~celery.task.base.Task.acks_late` option set).
  30. Also, since the ``KILL`` signal can't be catched by processes the worker will
  31. not be able to reap its children, so make sure you do it manually. This
  32. command usually does the trick::
  33. $ ps auxww | grep celeryd | awk '{print $2}' | xargs kill -KILL
  34. Restarting the worker
  35. =====================
  36. Other than stopping then starting the worker to restart, you can also
  37. restart the worker using the ``HUP`` signal::
  38. $ kill -HUP $pid
  39. The worker will then replace itself using the same arguments as it was
  40. started with.
  41. Remote control
  42. ==============
  43. Workers have the ability to be remote controlled using a broadcast message
  44. queue. The commands can be directed to all, or a specific list of workers.
  45. Commands can also have replies, the client can then wait for and collect
  46. those replies, but since there's no central authority to know how many
  47. workers are available in the cluster, there is also no way to estimate
  48. how many workers may send a reply, therefore the client has a configurable
  49. timeout - the deadline in seconds for replies to arrive in. This timeout
  50. defaults to one second. If the worker didn't reply within the deadline,
  51. it doesn't necessarily mean the worker didn't reply, or worse is dead, but
  52. may just be caused by network latency or the worker being slow at processing
  53. commands, so adjust the timeout accordingly.
  54. In addition to timeouts, the client can specify the maximum number
  55. of replies to wait for. If a destination is specified this limit is set
  56. to the number of destinations.
  57. The :func:`~celery.task.control.broadcast` function.
  58. ----------------------------------------------------
  59. This is the client function used to send commands to the workers.
  60. Some remote control commands also have higher-level interfaces using
  61. :func:`~celery.task.control.broadcast` in the background, like
  62. :func:`~celery.task.control.rate_limit` and :func:`~celery.task.control.ping`.
  63. Sending the ``rate_limit`` command and keyword arguments::
  64. >>> from celery.task.control import broadcast
  65. >>> broadcast("rate_limit", arguments={"task_name": "myapp.mytask",
  66. ... "rate_limit": "200/m"})
  67. This will send the command asynchronously, without waiting for a reply.
  68. To request a reply you have to use the ``reply`` argument::
  69. >>> broadcast("rate_limit", {"task_name": "myapp.mytask",
  70. ... "rate_limit": "200/m"}, reply=True)
  71. [{'worker1.example.com': 'New rate limit set successfully'},
  72. {'worker2.example.com': 'New rate limit set successfully'},
  73. {'worker3.example.com': 'New rate limit set successfully'}]
  74. Using the ``destination`` argument you can specify a list of workers
  75. to receive the command::
  76. >>> broadcast
  77. >>> broadcast("rate_limit", {"task_name": "myapp.mytask",
  78. ... "rate_limit": "200/m"}, reply=True,
  79. ... destination=["worker1.example.com"])
  80. [{'worker1.example.com': 'New rate limit set successfully'}]
  81. Of course, using the higher-level interface to set rate limits is much
  82. more convenient, but there are commands that can only be requested
  83. using :func:`~celery.task.control.broadcast`.
  84. Rate limits
  85. -----------
  86. Example changing the rate limit for the ``myapp.mytask`` task to accept
  87. 200 tasks a minute on all servers:
  88. >>> from celery.task.control import rate_limit
  89. >>> rate_limit("myapp.mytask", "200/m")
  90. Example changing the rate limit on a single host by specifying the
  91. destination hostname::
  92. >>> rate_limit("myapp.mytask", "200/m",
  93. ... destination=["worker1.example.com"])
  94. **NOTE** This won't affect workers with the ``CELERY_DISABLE_RATE_LIMITS``
  95. setting on. To re-enable rate limits you have to restart the worker.
  96. Remote shutdown
  97. ---------------
  98. This command will gracefully shut down the worker from remote.
  99. >>> broadcast("shutdown") # shutdown all workers
  100. >>> broadcast("shutdown, destination="worker1.example.com")
  101. Ping
  102. ----
  103. This command requests a ping from alive workers.
  104. The workers reply with the string 'pong', and that's just about it.
  105. It will use the default one second limit for replies unless you specify
  106. a custom ``timeout``.
  107. >>> from celery.task.control import ping
  108. >>> ping()
  109. [{'worker1.example.com': 'pong'},
  110. {'worker2.example.com': 'pong'},
  111. {'worker3.example.com': 'pong'}]
  112. :func:`~celery.task.control.ping` also supports the ``destination`` argument,
  113. so you can specify which workers to ping::
  114. >>> ping(['worker2.example.com', 'worker3.example.com'])
  115. [{'worker2.example.com': 'pong'},
  116. {'worker3.example.com': 'pong'}]
  117. Writing your own remote control commands
  118. ----------------------------------------
  119. Remote control commands are registered in the control panel and
  120. they take a single argument: the current
  121. :class:`~celery.worker.control.ControlDispatch` instance.
  122. From there you have access to the active
  123. :class:`celery.worker.listener.CarrotListener` if needed.
  124. Here's an example control command that restarts the broker connection:
  125. .. code-block:: python
  126. from celery.worker.control import Panel
  127. @Panel.register
  128. def reset_connection(panel):
  129. panel.logger.critical("Connection reset by remote control.")
  130. panel.listener.reset_connection()
  131. return {"ok": "connection reset"}
  132. These can be added to task modules, or you can keep them in their own module
  133. then import them using the ``CELERY_IMPORTS`` setting::
  134. CELERY_IMPORTS = ("myapp.worker.control", )
  135. Debugging
  136. =========
  137. Dump of registered tasks
  138. ------------------------
  139. You can get a list of tasks registered in the worker using the
  140. ``dump_tasks`` remote control command::
  141. >>> broadcast("dump_tasks", reply=True)
  142. [{'worker1.example.com': ['celery.delete_expired_task_meta',
  143. 'celery.execute_remote',
  144. 'celery.map_async',
  145. 'celery.ping',
  146. 'celery.task.http.HttpDispatchTask',
  147. 'tasks.add',
  148. 'tasks.sleeptask']}]
  149. Dump of scheduled (ETA) tasks
  150. -----------------------------
  151. You can get a list of tasks waiting to be scheduled by using
  152. the ``dump_schedule`` remote control command.
  153. >>> broadcast("dump_schedule", reply=True)
  154. [{'worker1.example.com':
  155. ['0. 2010-06-07 09:07:52 pri0 <TaskRequest: {
  156. name:"tasks.sleeptask",
  157. id:"1a7980ea-8b19-413e-91d2-0b74f3844c4d",
  158. args:"[1]", kwargs:"{}"}>',
  159. '1. 2010-06-07 09:07:53 pri0 <TaskRequest: {
  160. name:"tasks.sleeptask",
  161. id:"49661b9a-aa22-4120-94b7-9ee8031d219d",
  162. args:"[2]",
  163. kwargs:"{}"}>',
  164. The outputted fields are (in order): position, eta, priority, request.
  165. Note that these are tasks with an eta/countdown argument, not periodic tasks.
  166. Dump of reserved tasks
  167. ----------------------
  168. Reserved tasks are tasks that has been received by the broker and is waiting
  169. for immediate execution.
  170. You can get a list of these using the ``dump_reserved`` remote control command.
  171. >>> broadcast("dump_reserved", reply=True)
  172. [{'worker1.example.com':
  173. ['<TaskRequest: {name:"tasks.sleeptask",
  174. id:"32666e9b-809c-41fa-8e93-5ae0c80afbbf",
  175. args:"(8,)", kwargs:"{}"}>']}]