| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481 | .. _guide-worker:=============== Workers Guide===============.. contents::    :local:.. _worker-starting:Starting the worker===================You can start celeryd to run in the foreground by executing the command::    $ celeryd --loglevel=INFOYou probably want to use a daemonization tool to start`celeryd` in the background.  See :ref:`daemonizing` for helpusing `celeryd` with popular daemonization tools.For a full list of available command line options see:mod:`~celery.bin.celeryd`, or simply do::    $ celeryd --helpYou can also start multiple workers on the same machine. If you do sobe sure to give a unique name to each individual worker by specifying ahost name with the :option:`--hostname|-n` argument::    $ celeryd --loglevel=INFO --concurrency=10 -n worker1.example.com    $ celeryd --loglevel=INFO --concurrency=10 -n worker2.example.com    $ celeryd --loglevel=INFO --concurrency=10 -n worker3.example.com.. _worker-stopping:Stopping the worker===================Shutdown should be accomplished using the :sig:`TERM` signal.When shutdown is initiated the worker will finish all currently executingtasks before it actually terminates, so if these tasks are important you shouldwait for it to finish before doing anything drastic (like sending the :sig:`KILL`signal).If the worker won't shutdown after considerate time, for example becauseof tasks stuck in an infinite-loop, you can use the :sig:`KILL` signal toforce terminate the worker, but be aware that currently executing tasks willbe lost (unless the tasks have the :attr:`~celery.task.base.Task.acks_late`option set).Also as processes can't override the :sig:`KILL` signal, the worker willnot be able to reap its children, so make sure to do so manually.  Thiscommand usually does the trick::    $ ps auxww | grep celeryd | awk '{print $2}' | xargs kill -9.. _worker-restarting:Restarting the worker=====================Other than stopping then starting the worker to restart, you can alsorestart the worker using the :sig:`HUP` signal::    $ kill -HUP $pidThe worker will then replace itself with a new instance using the samearguments as it was started with... _worker-concurrency:Concurrency===========By default multiprocessing is used to perform concurrent execution of tasks,but you can also use :ref:`Eventlet <concurrency-eventlet>`.  The numberof worker processes/threads can be changed using the :option:`--concurrency`argument and defaults to the number of CPUs available on the machine... admonition:: Number of processes (multiprocessing)    More worker processes are usually better, but there's a cut-off point where    adding more processes affects performance in negative ways.    There is even some evidence to support that having multiple celeryd's running,    may perform better than having a single worker.  For example 3 celeryd's with    10 worker processes each.  You need to experiment to find the numbers that    works best for you, as this varies based on application, work load, task    run times and other factors... _worker-persistent-revokes:Persistent revokes==================Revoking tasks works by sending a broadcast message to all the workers,the workers then keep a list of revoked tasks in memory.If you want tasks to remain revoked after worker restart you need tospecify a file for these to be stored in, either by using the `--statedb`argument to :mod:`~celery.bin.celeryd` or the :setting:`CELERYD_STATE_DB`setting.  See :setting:`CELERYD_STATE_DB` for more information... _worker-time-limits:Time limits===========.. versionadded:: 2.0A single task can potentially run forever, if you have lots of taskswaiting for some event that will never happen you will block the workerfrom processing new tasks indefinitely.  The best way to defend againstthis scenario happening is enabling time limits.The time limit (`--time-limit`) is the maximum number of seconds a taskmay run before the process executing it is terminated and replaced by anew process.  You can also enable a soft time limit (`--soft-time-limit`),this raises an exception the task can catch to clean up before the hardtime limit kills it:.. code-block:: python    from celery.task import task    from celery.exceptions import SoftTimeLimitExceeded    @task()    def mytask():        try:            do_work()        except SoftTimeLimitExceeded:            clean_up_in_a_hurry()Time limits can also be set using the :setting:`CELERYD_TASK_TIME_LIMIT` /:setting:`CELERYD_SOFT_TASK_TIME_LIMIT` settings... note::    Time limits do not currently work on Windows and other    platforms that do not support the ``SIGUSR1`` signal.Changing time limits at runtime-------------------------------.. versionadded:: 2.3You can change the soft and hard time limits for a task by using the``time_limit`` remote control command.Example changing the time limit for the ``tasks.crawl_the_web`` taskto have a soft time limit of one minute, and a hard time limit oftwo minutes::    >>> from celery.task import control    >>> control.time_limit("tasks.crawl_the_web",                           soft=60, hard=120, reply=True)    [{'worker1.example.com': {'ok': 'time limits set successfully'}}]Only tasks that starts executing after the time limit change will be affected... _worker-maxtasksperchild:Max tasks per child setting===========================.. versionadded: 2.0With this option you can configure the maximum number of tasksa worker can execute before it's replaced by a new process.This is useful if you have memory leaks you have no control overfor example from closed source C extensions.The option can be set using the `--maxtasksperchild` argumentto `celeryd` or using the :setting:`CELERYD_MAX_TASKS_PER_CHILD` setting... _worker-remote-control:Remote control==============.. versionadded:: 2.0Workers have the ability to be remote controlled using a high-prioritybroadcast message queue.  The commands can be directed to all, or a specificlist of workers.Commands can also have replies.  The client can then wait for and collectthose replies.  Since there's no central authority to know how manyworkers are available in the cluster, there is also no way to estimatehow many workers may send a reply, so the client has a configurabletimeout — the deadline in seconds for replies to arrive in.  This timeoutdefaults to one second.  If the worker doesn't reply within the deadlineit doesn't necessarily mean the worker didn't reply, or worse is dead, butmay simply be caused by network latency or the worker being slow at processingcommands, so adjust the timeout accordingly.In addition to timeouts, the client can specify the maximum numberof replies to wait for.  If a destination is specified, this limit is setto the number of destination hosts... seealso::    The :program:`celeryctl` program is used to execute remote control    commands from the command line.  It supports all of the commands    listed below.  See :ref:`monitoring-celeryctl` for more information... _worker-broadcast-fun:The :func:`~celery.task.control.broadcast` function.----------------------------------------------------This is the client function used to send commands to the workers.Some remote control commands also have higher-level interfaces using:func:`~celery.task.control.broadcast` in the background, like:func:`~celery.task.control.rate_limit` and :func:`~celery.task.control.ping`.Sending the :control:`rate_limit` command and keyword arguments::    >>> from celery.task.control import broadcast    >>> broadcast("rate_limit", arguments={"task_name": "myapp.mytask",    ...                                    "rate_limit": "200/m"})This will send the command asynchronously, without waiting for a reply.To request a reply you have to use the `reply` argument::    >>> broadcast("rate_limit", {"task_name": "myapp.mytask",    ...                          "rate_limit": "200/m"}, reply=True)    [{'worker1.example.com': 'New rate limit set successfully'},     {'worker2.example.com': 'New rate limit set successfully'},     {'worker3.example.com': 'New rate limit set successfully'}]Using the `destination` argument you can specify a list of workersto receive the command::    >>> broadcast    >>> broadcast("rate_limit", {"task_name": "myapp.mytask",    ...                          "rate_limit": "200/m"}, reply=True,    ...           destination=["worker1.example.com"])    [{'worker1.example.com': 'New rate limit set successfully'}]Of course, using the higher-level interface to set rate limits is muchmore convenient, but there are commands that can only be requestedusing :func:`~celery.task.control.broadcast`... _worker-rate-limits:.. control:: rate_limitRate limits-----------Example changing the rate limit for the `myapp.mytask` task to accept200 tasks a minute on all servers::    >>> from celery.task.control import rate_limit    >>> rate_limit("myapp.mytask", "200/m")Example changing the rate limit on a single host by specifying thedestination host name::    >>> rate_limit("myapp.mytask", "200/m",    ...            destination=["worker1.example.com"]).. warning::    This won't affect workers with the    :setting:`CELERY_DISABLE_RATE_LIMITS` setting on. To re-enable rate limits    then you have to restart the worker... control:: revokeRevoking tasks--------------All worker nodes keeps a memory of revoked task ids, either in-memory orpersistent on disk (see :ref:`worker-persistent-revokes`).When a worker receives a revoke request it will skip executingthe task, but it won't terminate an already executing task unlessthe `terminate` option is set.If `terminate` is set the worker child process processing the taskwill be terminated.  The default signal sent is `TERM`, but you canspecify this using the `signal` argument.  Signal can be the uppercase nameof any signal defined in the :mod:`signal` module in the Python StandardLibrary.Terminating a task also revokes it.**Example**::    >>> from celery.task.control import revoke    >>> revoke("d9078da5-9915-40a0-bfa1-392c7bde42ed")    >>> revoke("d9078da5-9915-40a0-bfa1-392c7bde42ed",    ...        terminate=True)    >>> revoke("d9078da5-9915-40a0-bfa1-392c7bde42ed",    ...        terminate=True, signal="SIGKILL").. control:: shutdownRemote shutdown---------------This command will gracefully shut down the worker remotely::    >>> broadcast("shutdown") # shutdown all workers    >>> broadcast("shutdown, destination="worker1.example.com").. control:: pingPing----This command requests a ping from alive workers.The workers reply with the string 'pong', and that's just about it.It will use the default one second timeout for replies unless you specifya custom timeout::    >>> from celery.task.control import ping    >>> ping(timeout=0.5)    [{'worker1.example.com': 'pong'},     {'worker2.example.com': 'pong'},     {'worker3.example.com': 'pong'}]:func:`~celery.task.control.ping` also supports the `destination` argument,so you can specify which workers to ping::    >>> ping(['worker2.example.com', 'worker3.example.com'])    [{'worker2.example.com': 'pong'},     {'worker3.example.com': 'pong'}].. _worker-enable-events:.. control:: enable_events.. control:: disable_eventsEnable/disable events---------------------You can enable/disable events by using the `enable_events`,`disable_events` commands.  This is useful to temporarily monitora worker using :program:`celeryev`/:program:`celerymon`... code-block:: python    >>> broadcast("enable_events")    >>> broadcast("disable_events").. _worker-custom-control-commands:Writing your own remote control commands----------------------------------------Remote control commands are registered in the control panel andthey take a single argument: the current:class:`~celery.worker.control.ControlDispatch` instance.From there you have access to the active:class:`~celery.worker.consumer.Consumer` if needed.Here's an example control command that restarts the broker connection:.. code-block:: python    from celery.worker.control import Panel    @Panel.register    def reset_connection(panel):        panel.logger.critical("Connection reset by remote control.")        panel.consumer.reset_connection()        return {"ok": "connection reset"}These can be added to task modules, or you can keep them in their own modulethen import them using the :setting:`CELERY_IMPORTS` setting::    CELERY_IMPORTS = ("myapp.worker.control", ).. _worker-inspect:Inspecting workers==================:class:`celery.task.control.inspect` lets you inspect running workers.  Ituses remote control commands under the hood... code-block:: python    >>> from celery.task.control import inspect    # Inspect all nodes.    >>> i = inspect()    # Specify multiple nodes to inspect.    >>> i = inspect(["worker1.example.com", "worker2.example.com"])    # Specify a single node to inspect.    >>> i = inspect("worker1.example.com").. _worker-inspect-registered-tasks:Dump of registered tasks------------------------You can get a list of tasks registered in the worker using the:meth:`~celery.task.control.inspect.registered_tasks`::    >>> i.registered_tasks()    [{'worker1.example.com': ['celery.delete_expired_task_meta',                              'celery.execute_remote',                              'celery.map_async',                              'celery.ping',                              'celery.task.http.HttpDispatchTask',                              'tasks.add',                              'tasks.sleeptask']}].. _worker-inspect-active-tasks:Dump of currently executing tasks---------------------------------You can get a list of active tasks using:meth:`~celery.task.control.inspect.active`::    >>> i.active()    [{'worker1.example.com':        [{"name": "tasks.sleeptask",          "id": "32666e9b-809c-41fa-8e93-5ae0c80afbbf",          "args": "(8,)",          "kwargs": "{}"}]}].. _worker-inspect-eta-schedule:Dump of scheduled (ETA) tasks-----------------------------You can get a list of tasks waiting to be scheduled by using:meth:`~celery.task.control.inspect.scheduled`::    >>> i.scheduled()    [{'worker1.example.com':        [{"eta": "2010-06-07 09:07:52", "priority": 0,          "request": {            "name": "tasks.sleeptask",            "id": "1a7980ea-8b19-413e-91d2-0b74f3844c4d",            "args": "[1]",            "kwargs": "{}"}},         {"eta": "2010-06-07 09:07:53", "priority": 0,          "request": {            "name": "tasks.sleeptask",            "id": "49661b9a-aa22-4120-94b7-9ee8031d219d",            "args": "[2]",            "kwargs": "{}"}}]}]Note that these are tasks with an eta/countdown argument, not periodic tasks... _worker-inspect-reserved:Dump of reserved tasks----------------------Reserved tasks are tasks that has been received, but is still waiting to beexecuted.You can get a list of these using:meth:`~celery.task.control.inspect.reserved`::    >>> i.reserved()    [{'worker1.example.com':        [{"name": "tasks.sleeptask",          "id": "32666e9b-809c-41fa-8e93-5ae0c80afbbf",          "args": "(8,)",          "kwargs": "{}"}]}]
 |