workers.rst 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. .. _guide-workers:
  2. ===============
  3. Workers Guide
  4. ===============
  5. .. contents::
  6. :local:
  7. :depth: 1
  8. .. _worker-starting:
  9. Starting the worker
  10. ===================
  11. .. sidebar:: Daemonizing
  12. You probably want to use a daemonization tool to start
  13. in the background. See :ref:`daemonizing` for help
  14. detaching the worker using popular daemonization tools.
  15. You can start the worker in the foreground by executing the command:
  16. .. code-block:: bash
  17. $ celery worker --app=app -l info
  18. For a full list of available command line options see
  19. :mod:`~celery.bin.celeryd`, or simply do:
  20. .. code-block:: bash
  21. $ celery worker --help
  22. You can also start multiple workers on the same machine. If you do so
  23. be sure to give a unique name to each individual worker by specifying a
  24. host name with the :option:`--hostname|-n` argument:
  25. .. code-block:: bash
  26. $ celery worker --loglevel=INFO --concurrency=10 -n worker1.example.com
  27. $ celery worker --loglevel=INFO --concurrency=10 -n worker2.example.com
  28. $ celery worker --loglevel=INFO --concurrency=10 -n worker3.example.com
  29. .. _worker-stopping:
  30. Stopping the worker
  31. ===================
  32. Shutdown should be accomplished using the :sig:`TERM` signal.
  33. When shutdown is initiated the worker will finish all currently executing
  34. tasks before it actually terminates, so if these tasks are important you should
  35. wait for it to finish before doing anything drastic (like sending the :sig:`KILL`
  36. signal).
  37. If the worker won't shutdown after considerate time, for example because
  38. of tasks stuck in an infinite-loop, you can use the :sig:`KILL` signal to
  39. force terminate the worker, but be aware that currently executing tasks will
  40. be lost (unless the tasks have the :attr:`~@Task.acks_late`
  41. option set).
  42. Also as processes can't override the :sig:`KILL` signal, the worker will
  43. not be able to reap its children, so make sure to do so manually. This
  44. command usually does the trick:
  45. .. code-block:: bash
  46. $ ps auxww | grep 'celery worker' | awk '{print $2}' | xargs kill -9
  47. .. _worker-restarting:
  48. Restarting the worker
  49. =====================
  50. Other than stopping then starting the worker to restart, you can also
  51. restart the worker using the :sig:`HUP` signal:
  52. .. code-block:: bash
  53. $ kill -HUP $pid
  54. The worker will then replace itself with a new instance using the same
  55. arguments as it was started with.
  56. .. note::
  57. Restarting by :sig:`HUP` only works if the worker is running
  58. in the background as a daemon (it does not have a controlling
  59. terminal).
  60. :sig:`HUP` is disabled on OS X because of a limitation on
  61. that platform.
  62. .. _worker-process-signals:
  63. Process Signals
  64. ===============
  65. The worker's main process overrides the following signals:
  66. +--------------+-------------------------------------------------+
  67. | :sig:`TERM` | Warm shutdown, wait for tasks to complete. |
  68. +--------------+-------------------------------------------------+
  69. | :sig:`QUIT` | Cold shutdown, terminate ASAP |
  70. +--------------+-------------------------------------------------+
  71. | :sig:`USR1` | Dump traceback for all active threads. |
  72. +--------------+-------------------------------------------------+
  73. | :sig:`USR2` | Remote debug, see :mod:`celery.contrib.rdb`. |
  74. +--------------+-------------------------------------------------+
  75. .. _worker-concurrency:
  76. Concurrency
  77. ===========
  78. By default multiprocessing is used to perform concurrent execution of tasks,
  79. but you can also use :ref:`Eventlet <concurrency-eventlet>`. The number
  80. of worker processes/threads can be changed using the :option:`--concurrency`
  81. argument and defaults to the number of CPUs available on the machine.
  82. .. admonition:: Number of processes (multiprocessing)
  83. More pool processes are usually better, but there's a cut-off point where
  84. adding more pool processes affects performance in negative ways.
  85. There is even some evidence to support that having multiple worker
  86. instances running, may perform better than having a single worker.
  87. For example 3 workers with 10 pool processes each. You need to experiment
  88. to find the numbers that works best for you, as this varies based on
  89. application, work load, task run times and other factors.
  90. .. _worker-remote-control:
  91. Remote control
  92. ==============
  93. .. versionadded:: 2.0
  94. .. sidebar:: The ``celery`` command
  95. The :program:`celery` program is used to execute remote control
  96. commands from the command line. It supports all of the commands
  97. listed below. See :ref:`monitoring-celeryctl` for more information.
  98. pool support: *processes, eventlet, gevent*, blocking:*threads/solo* (see note)
  99. broker support: *amqp, redis, mongodb*
  100. Workers have the ability to be remote controlled using a high-priority
  101. broadcast message queue. The commands can be directed to all, or a specific
  102. list of workers.
  103. Commands can also have replies. The client can then wait for and collect
  104. those replies. Since there's no central authority to know how many
  105. workers are available in the cluster, there is also no way to estimate
  106. how many workers may send a reply, so the client has a configurable
  107. timeout — the deadline in seconds for replies to arrive in. This timeout
  108. defaults to one second. If the worker doesn't reply within the deadline
  109. it doesn't necessarily mean the worker didn't reply, or worse is dead, but
  110. may simply be caused by network latency or the worker being slow at processing
  111. commands, so adjust the timeout accordingly.
  112. In addition to timeouts, the client can specify the maximum number
  113. of replies to wait for. If a destination is specified, this limit is set
  114. to the number of destination hosts.
  115. .. note::
  116. The solo and threads pool supports remote control commands,
  117. but any task executing will block any waiting control command,
  118. so it is of limited use if the worker is very busy. In that
  119. case you must increase the timeout waiting for replies in the client.
  120. .. _worker-broadcast-fun:
  121. The :meth:`~@control.broadcast` function.
  122. ----------------------------------------------------
  123. This is the client function used to send commands to the workers.
  124. Some remote control commands also have higher-level interfaces using
  125. :meth:`~@control.broadcast` in the background, like
  126. :meth:`~@control.rate_limit` and :meth:`~@control.ping`.
  127. Sending the :control:`rate_limit` command and keyword arguments::
  128. >>> celery.control.broadcast('rate_limit',
  129. ... arguments={'task_name': 'myapp.mytask',
  130. ... 'rate_limit': '200/m'})
  131. This will send the command asynchronously, without waiting for a reply.
  132. To request a reply you have to use the `reply` argument::
  133. >>> celery.control.broadcast('rate_limit', {
  134. ... 'task_name': 'myapp.mytask', 'rate_limit': '200/m'}, reply=True)
  135. [{'worker1.example.com': 'New rate limit set successfully'},
  136. {'worker2.example.com': 'New rate limit set successfully'},
  137. {'worker3.example.com': 'New rate limit set successfully'}]
  138. Using the `destination` argument you can specify a list of workers
  139. to receive the command::
  140. >>> celery.control.broadcast('rate_limit', {
  141. ... 'task_name': 'myapp.mytask',
  142. ... 'rate_limit': '200/m'}, reply=True,
  143. ... destination=['worker1.example.com'])
  144. [{'worker1.example.com': 'New rate limit set successfully'}]
  145. Of course, using the higher-level interface to set rate limits is much
  146. more convenient, but there are commands that can only be requested
  147. using :meth:`~@control.broadcast`.
  148. .. control:: revoke
  149. Revoking tasks
  150. ==============
  151. pool support: all
  152. broker support: *amqp, redis, mongodb*
  153. All worker nodes keeps a memory of revoked task ids, either in-memory or
  154. persistent on disk (see :ref:`worker-persistent-revokes`).
  155. When a worker receives a revoke request it will skip executing
  156. the task, but it won't terminate an already executing task unless
  157. the `terminate` option is set.
  158. If `terminate` is set the worker child process processing the task
  159. will be terminated. The default signal sent is `TERM`, but you can
  160. specify this using the `signal` argument. Signal can be the uppercase name
  161. of any signal defined in the :mod:`signal` module in the Python Standard
  162. Library.
  163. Terminating a task also revokes it.
  164. **Example**
  165. ::
  166. >>> celery.control.revoke('d9078da5-9915-40a0-bfa1-392c7bde42ed')
  167. >>> celery.control.revoke('d9078da5-9915-40a0-bfa1-392c7bde42ed',
  168. ... terminate=True)
  169. >>> celery.control.revoke('d9078da5-9915-40a0-bfa1-392c7bde42ed',
  170. ... terminate=True, signal='SIGKILL')
  171. .. _worker-persistent-revokes:
  172. Persistent revokes
  173. ------------------
  174. Revoking tasks works by sending a broadcast message to all the workers,
  175. the workers then keep a list of revoked tasks in memory.
  176. If you want tasks to remain revoked after worker restart you need to
  177. specify a file for these to be stored in, either by using the `--statedb`
  178. argument to :program:`celery worker` or the :setting:`CELERYD_STATE_DB`
  179. setting.
  180. Note that remote control commands must be working for revokes to work.
  181. Remote control commands are only supported by the RabbitMQ (amqp), Redis and MongDB
  182. transports at this point.
  183. .. _worker-time-limits:
  184. Time Limits
  185. ===========
  186. .. versionadded:: 2.0
  187. pool support: *processes*
  188. .. sidebar:: Soft, or hard?
  189. The time limit is set in two values, `soft` and `hard`.
  190. The soft time limit allows the task to catch an exception
  191. to clean up before it is killed: the hard timeout is not catchable
  192. and force terminates the task.
  193. A single task can potentially run forever, if you have lots of tasks
  194. waiting for some event that will never happen you will block the worker
  195. from processing new tasks indefinitely. The best way to defend against
  196. this scenario happening is enabling time limits.
  197. The time limit (`--time-limit`) is the maximum number of seconds a task
  198. may run before the process executing it is terminated and replaced by a
  199. new process. You can also enable a soft time limit (`--soft-time-limit`),
  200. this raises an exception the task can catch to clean up before the hard
  201. time limit kills it:
  202. .. code-block:: python
  203. from myapp import celery
  204. from celery.exceptions import SoftTimeLimitExceeded
  205. @celery.task
  206. def mytask():
  207. try:
  208. do_work()
  209. except SoftTimeLimitExceeded:
  210. clean_up_in_a_hurry()
  211. Time limits can also be set using the :setting:`CELERYD_TASK_TIME_LIMIT` /
  212. :setting:`CELERYD_SOFT_TASK_TIME_LIMIT` settings.
  213. .. note::
  214. Time limits do not currently work on Windows and other
  215. platforms that do not support the ``SIGUSR1`` signal.
  216. Changing time limits at runtime
  217. -------------------------------
  218. .. versionadded:: 2.3
  219. broker support: *amqp, redis, mongodb*
  220. There is a remote control command that enables you to change both soft
  221. and hard time limits for a task — named ``time_limit``.
  222. Example changing the time limit for the ``tasks.crawl_the_web`` task
  223. to have a soft time limit of one minute, and a hard time limit of
  224. two minutes::
  225. >>> celery.control.time_limit('tasks.crawl_the_web',
  226. soft=60, hard=120, reply=True)
  227. [{'worker1.example.com': {'ok': 'time limits set successfully'}}]
  228. Only tasks that starts executing after the time limit change will be affected.
  229. .. _worker-rate-limits:
  230. Rate Limits
  231. ===========
  232. .. control:: rate_limit
  233. Changing rate-limits at runtime
  234. -------------------------------
  235. Example changing the rate limit for the `myapp.mytask` task to accept
  236. 200 tasks a minute on all servers::
  237. >>> celery.control.rate_limit('myapp.mytask', '200/m')
  238. Example changing the rate limit on a single host by specifying the
  239. destination host name::
  240. >>> celery.control.rate_limit('myapp.mytask', '200/m',
  241. ... destination=['worker1.example.com'])
  242. .. warning::
  243. This won't affect workers with the
  244. :setting:`CELERY_DISABLE_RATE_LIMITS` setting enabled.
  245. .. _worker-maxtasksperchild:
  246. Max tasks per child setting
  247. ===========================
  248. .. versionadded:: 2.0
  249. pool support: *processes*
  250. With this option you can configure the maximum number of tasks
  251. a worker can execute before it's replaced by a new process.
  252. This is useful if you have memory leaks you have no control over
  253. for example from closed source C extensions.
  254. The option can be set using the workers `--maxtasksperchild` argument
  255. or using the :setting:`CELERYD_MAX_TASKS_PER_CHILD` setting.
  256. .. _worker-autoscaling:
  257. Autoscaling
  258. ===========
  259. .. versionadded:: 2.2
  260. pool support: *processes*, *gevent*
  261. The *autoscaler* component is used to dynamically resize the pool
  262. based on load:
  263. - The autoscaler adds more pool processes when there is work to do,
  264. - and starts removing processes when the workload is low.
  265. It's enabled by the :option:`--autoscale` option, which needs two
  266. numbers: the maximum and minimum number of pool processes::
  267. --autoscale=AUTOSCALE
  268. Enable autoscaling by providing
  269. max_concurrency,min_concurrency. Example:
  270. --autoscale=10,3 (always keep 3 processes, but grow to
  271. 10 if necessary).
  272. You can also define your own rules for the autoscaler by subclassing
  273. :class:`~celery.worker.autoscaler.Autoscaler`.
  274. Some ideas for metrics include load average or the amount of memory available.
  275. You can specify a custom autoscaler with the :setting:`CELERYD_AUTOSCALER` setting.
  276. .. _worker-queues:
  277. Queues
  278. ======
  279. A worker instance can consume from any number of queues.
  280. By default it will consume from all queues defined in the
  281. :setting:`CELERY_QUEUES` setting (which if not specified defaults to the
  282. queue named ``celery``).
  283. You can specify what queues to consume from at startup,
  284. by giving a comma separated list of queues to the :option:`-Q` option:
  285. .. code-block:: bash
  286. $ celery worker -l info -Q foo,bar,baz
  287. If the queue name is defined in :setting:`CELERY_QUEUES` it will use that
  288. configuration, but if it's not defined in the list of queues Celery will
  289. automatically generate a new queue for you (depending on the
  290. :setting:`CELERY_CREATE_MISSING_QUEUES` option).
  291. You can also tell the worker to start and stop consuming from a queue at
  292. runtime using the remote control commands :control:`add_consumer` and
  293. :control:`cancel_consumer`.
  294. .. control:: add_consumer
  295. Queues: Adding consumers
  296. ------------------------
  297. The :control:`add_consumer` control command will tell one or more workers
  298. to start consuming from a queue. This operation is idempotent.
  299. To tell all workers in the cluster to start consuming from a queue
  300. named "``foo``" you can use the :program:`celery control` program:
  301. .. code-block:: bash
  302. $ celery control add_consumer foo
  303. -> worker1.local: OK
  304. started consuming from u'foo'
  305. If you want to specify a specific worker you can use the
  306. :option:`--destination`` argument:
  307. .. code-block:: bash
  308. $ celery control add_consumer foo -d worker1.local
  309. The same can be accomplished dynamically using the :meth:`@control.add_consumer` method::
  310. >>> myapp.control.add_consumer('foo', reply=True)
  311. [{u'worker1.local': {u'ok': u"already consuming from u'foo'"}}]
  312. >>> myapp.control.add_consumer('foo', reply=True,
  313. ... destination=['worker1.local'])
  314. [{u'worker1.local': {u'ok': u"already consuming from u'foo'"}}]
  315. By now I have only shown examples using automatic queues,
  316. If you need more control you can also specify the exchange, routing_key and
  317. even other options::
  318. >>> myapp.control.add_consumer(
  319. ... queue='baz',
  320. ... exchange='ex',
  321. ... exchange_type='topic',
  322. ... routing_key='media.*',
  323. ... options={
  324. ... 'queue_durable': False,
  325. ... 'exchange_durable': False,
  326. ... },
  327. ... reply=True,
  328. ... destination=['worker1.local', 'worker2.local'])
  329. .. control:: cancel_consumer
  330. Queues: Cancelling consumers
  331. ----------------------------
  332. You can cancel a consumer by queue name using the :control:`cancel_consumer`
  333. control command.
  334. To force all workers in the cluster to cancel consuming from a queue
  335. you can use the :program:`celery control` program:
  336. .. code-block:: bash
  337. $ celery control cancel_consumer foo
  338. The :option:`--destination` argument can be used to specify a worker, or a
  339. list of workers, to act on the command:
  340. .. code-block:: bash
  341. $ celery control cancel_consumer foo -d worker1.local
  342. You can also cancel consumers programmatically using the
  343. :meth:`@control.cancel_consumer` method:
  344. .. code-block:: bash
  345. >>> myapp.control.cancel_consumer('foo', reply=True)
  346. [{u'worker1.local': {u'ok': u"no longer consuming from u'foo'"}}]
  347. .. control:: active_queues
  348. Queues: List of active queues
  349. -----------------------------
  350. You can get a list of queues that a worker consumes from by using
  351. the :control:`active_queues` control command:
  352. .. code-block:: bash
  353. $ celery inspect active_queues
  354. [...]
  355. Like all other remote control commands this also supports the
  356. :option:`--destination` argument used to specify which workers should
  357. reply to the request:
  358. .. code-block:: bash
  359. $ celery inspect active_queues -d worker1.local
  360. [...]
  361. This can also be done programmatically by using the
  362. :meth:`@control.inspect.active_queues` method::
  363. >>> myapp.inspect().active_queues()
  364. [...]
  365. >>> myapp.inspect(['worker1.local']).active_queues()
  366. [...]
  367. .. _worker-autoreloading:
  368. Autoreloading
  369. =============
  370. .. versionadded:: 2.5
  371. pool support: *processes, eventlet, gevent, threads, solo*
  372. Starting :program:`celery worker` with the :option:`--autoreload` option will
  373. enable the worker to watch for file system changes to all imported task
  374. modules imported (and also any non-task modules added to the
  375. :setting:`CELERY_IMPORTS` setting or the :option:`-I|--include` option).
  376. This is an experimental feature intended for use in development only,
  377. using auto-reload in production is discouraged as the behavior of reloading
  378. a module in Python is undefined, and may cause hard to diagnose bugs and
  379. crashes. Celery uses the same approach as the auto-reloader found in e.g.
  380. the Django ``runserver`` command.
  381. When auto-reload is enabled the worker starts an additional thread
  382. that watches for changes in the file system. New modules are imported,
  383. and already imported modules are reloaded whenever a change is detected,
  384. and if the processes pool is used the child processes will finish the work
  385. they are doing and exit, so that they can be replaced by fresh processes
  386. effectively reloading the code.
  387. File system notification backends are pluggable, and it comes with three
  388. implementations:
  389. * inotify (Linux)
  390. Used if the :mod:`pyinotify` library is installed.
  391. If you are running on Linux this is the recommended implementation,
  392. to install the :mod:`pyinotify` library you have to run the following
  393. command:
  394. .. code-block:: bash
  395. $ pip install pyinotify
  396. * kqueue (OS X/BSD)
  397. * stat
  398. The fallback implementation simply polls the files using ``stat`` and is very
  399. expensive.
  400. You can force an implementation by setting the :envvar:`CELERYD_FSNOTIFY`
  401. environment variable:
  402. .. code-block:: bash
  403. $ env CELERYD_FSNOTIFY=stat celery worker -l info --autoreload
  404. .. _worker-autoreload:
  405. .. control:: pool_restart
  406. Pool Restart Command
  407. --------------------
  408. .. versionadded:: 2.5
  409. Requires the :setting:`CELERYD_POOL_RESTARTS` setting to be enabled.
  410. The remote control command :control:`pool_restart` sends restart requests to
  411. the workers child processes. It is particularly useful for forcing
  412. the worker to import new modules, or for reloading already imported
  413. modules. This command does not interrupt executing tasks.
  414. Example
  415. ~~~~~~~
  416. Running the following command will result in the `foo` and `bar` modules
  417. being imported by the worker processes:
  418. .. code-block:: python
  419. >>> celery.control.broadcast('pool_restart',
  420. ... arguments={'modules': ['foo', 'bar']})
  421. Use the ``reload`` argument to reload modules it has already imported:
  422. .. code-block:: python
  423. >>> celery.control.broadcast('pool_restart',
  424. ... arguments={'modules': ['foo'],
  425. ... 'reload': True})
  426. If you don't specify any modules then all known tasks modules will
  427. be imported/reloaded:
  428. .. code-block:: python
  429. >>> celery.control.broadcast('pool_restart', arguments={'reload': True})
  430. The ``modules`` argument is a list of modules to modify. ``reload``
  431. specifies whether to reload modules if they have previously been imported.
  432. By default ``reload`` is disabled. The `pool_restart` command uses the
  433. Python :func:`reload` function to reload modules, or you can provide
  434. your own custom reloader by passing the ``reloader`` argument.
  435. .. note::
  436. Module reloading comes with caveats that are documented in :func:`reload`.
  437. Please read this documentation and make sure your modules are suitable
  438. for reloading.
  439. .. seealso::
  440. - http://pyunit.sourceforge.net/notes/reloading.html
  441. - http://www.indelible.org/ink/python-reloading/
  442. - http://docs.python.org/library/functions.html#reload
  443. .. _worker-inspect:
  444. Inspecting workers
  445. ==================
  446. :class:`@control.inspect` lets you inspect running workers. It
  447. uses remote control commands under the hood.
  448. You can also use the ``celery`` command to inspect workers,
  449. and it supports the same commands as the :class:`@Celery.control` interface.
  450. .. code-block:: python
  451. # Inspect all nodes.
  452. >>> i = celery.control.inspect()
  453. # Specify multiple nodes to inspect.
  454. >>> i = celery.control.inspect(['worker1.example.com',
  455. 'worker2.example.com'])
  456. # Specify a single node to inspect.
  457. >>> i = celery.control.inspect('worker1.example.com')
  458. .. _worker-inspect-registered-tasks:
  459. Dump of registered tasks
  460. ------------------------
  461. You can get a list of tasks registered in the worker using the
  462. :meth:`~@control.inspect.registered`::
  463. >>> i.registered()
  464. [{'worker1.example.com': ['tasks.add',
  465. 'tasks.sleeptask']}]
  466. .. _worker-inspect-active-tasks:
  467. Dump of currently executing tasks
  468. ---------------------------------
  469. You can get a list of active tasks using
  470. :meth:`~@control.inspect.active`::
  471. >>> i.active()
  472. [{'worker1.example.com':
  473. [{'name': 'tasks.sleeptask',
  474. 'id': '32666e9b-809c-41fa-8e93-5ae0c80afbbf',
  475. 'args': '(8,)',
  476. 'kwargs': '{}'}]}]
  477. .. _worker-inspect-eta-schedule:
  478. Dump of scheduled (ETA) tasks
  479. -----------------------------
  480. You can get a list of tasks waiting to be scheduled by using
  481. :meth:`~@control.inspect.scheduled`::
  482. >>> i.scheduled()
  483. [{'worker1.example.com':
  484. [{'eta': '2010-06-07 09:07:52', 'priority': 0,
  485. 'request': {
  486. 'name': 'tasks.sleeptask',
  487. 'id': '1a7980ea-8b19-413e-91d2-0b74f3844c4d',
  488. 'args': '[1]',
  489. 'kwargs': '{}'}},
  490. {'eta': '2010-06-07 09:07:53', 'priority': 0,
  491. 'request': {
  492. 'name': 'tasks.sleeptask',
  493. 'id': '49661b9a-aa22-4120-94b7-9ee8031d219d',
  494. 'args': '[2]',
  495. 'kwargs': '{}'}}]}]
  496. .. note::
  497. These are tasks with an eta/countdown argument, not periodic tasks.
  498. .. _worker-inspect-reserved:
  499. Dump of reserved tasks
  500. ----------------------
  501. Reserved tasks are tasks that has been received, but is still waiting to be
  502. executed.
  503. You can get a list of these using
  504. :meth:`~@control.inspect.reserved`::
  505. >>> i.reserved()
  506. [{'worker1.example.com':
  507. [{'name': 'tasks.sleeptask',
  508. 'id': '32666e9b-809c-41fa-8e93-5ae0c80afbbf',
  509. 'args': '(8,)',
  510. 'kwargs': '{}'}]}]
  511. Additional Commands
  512. ===================
  513. .. control:: shutdown
  514. Remote shutdown
  515. ---------------
  516. This command will gracefully shut down the worker remotely::
  517. >>> celery.control.broadcast('shutdown') # shutdown all workers
  518. >>> celery.control.broadcast('shutdown, destination='worker1.example.com')
  519. .. control:: ping
  520. Ping
  521. ----
  522. This command requests a ping from alive workers.
  523. The workers reply with the string 'pong', and that's just about it.
  524. It will use the default one second timeout for replies unless you specify
  525. a custom timeout::
  526. >>> celery.control.ping(timeout=0.5)
  527. [{'worker1.example.com': 'pong'},
  528. {'worker2.example.com': 'pong'},
  529. {'worker3.example.com': 'pong'}]
  530. :meth:`~@control.ping` also supports the `destination` argument,
  531. so you can specify which workers to ping::
  532. >>> ping(['worker2.example.com', 'worker3.example.com'])
  533. [{'worker2.example.com': 'pong'},
  534. {'worker3.example.com': 'pong'}]
  535. .. _worker-enable-events:
  536. .. control:: enable_events
  537. .. control:: disable_events
  538. Enable/disable events
  539. ---------------------
  540. You can enable/disable events by using the `enable_events`,
  541. `disable_events` commands. This is useful to temporarily monitor
  542. a worker using :program:`celery events`/:program:`celerymon`.
  543. .. code-block:: python
  544. >>> celery.control.enable_events()
  545. >>> celery.control.disable_events()
  546. .. _worker-custom-control-commands:
  547. Writing your own remote control commands
  548. ========================================
  549. Remote control commands are registered in the control panel and
  550. they take a single argument: the current
  551. :class:`~celery.worker.control.ControlDispatch` instance.
  552. From there you have access to the active
  553. :class:`~celery.worker.consumer.Consumer` if needed.
  554. Here's an example control command that restarts the broker connection:
  555. .. code-block:: python
  556. from celery.worker.control import Panel
  557. @Panel.register
  558. def reset_connection(panel):
  559. panel.logger.critical('Connection reset by remote control.')
  560. panel.consumer.reset_connection()
  561. return {'ok': 'connection reset'}