| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732 | .. _faq:============================ Frequently Asked Questions============================.. contents::    :local:.. _faq-general:General=======.. _faq-when-to-use:What kinds of things should I use Celery for?---------------------------------------------**Answer:** `Queue everything and delight everyone`_ is a good articledescribing why you would use a queue in a web context... _`Queue everything and delight everyone`:    http://decafbad.com/blog/2008/07/04/queue-everything-and-delight-everyoneThese are some common use cases:* Running something in the background. For example, to finish the web request  as soon as possible, then update the users page incrementally.  This gives the user the impression of good performane and "snappiness", even  though the real work might actually take some time.* Running something after the web request has finished.* Making sure something is done, by executing it asynchronously and using  retries.* Scheduling periodic work.And to some degree:* Distributed computing.* Parallel execution... _faq-misconceptions:Misconceptions==============.. _faq-serializion-is-a-choice:Is Celery dependent on pickle?------------------------------**Answer:** No.Celery can support any serialization scheme and has support for JSON/YAML andPickle by default. You can even send one task using pickle, and another onewith JSON seamlessly, this is because every task is associated with acontent-type. The default serialization scheme is pickle because it's the mostused, and it has support for sending complex objects as task arguments.You can set a global default serializer, the default serializer for aparticular Task, or even what serializer to use when sending a single taskinstance... _faq-is-celery-for-django-only:Is Celery for Django only?--------------------------**Answer:** No.Celery does not depend on Django anymore. To use Celery with Django you haveto use the `django-celery`_ package... _`django-celery`: http://pypi.python.org/pypi/django-celery.. _faq-is-celery-for-rabbitmq-only:Do I have to use AMQP/RabbitMQ?-------------------------------**Answer**: No.You can also use Redis or an SQL database, see `Using otherqueues`_... _`Using other queues`:    http://ask.github.com/celery/tutorials/otherqueues.htmlRedis or a database won't perform as well asan AMQP broker. If you have strict reliability requirements you areencouraged to use RabbitMQ or another AMQP broker. Redis/database also usepolling, so they are likely to consume more resources. However, if you forsome reason are not able to use AMQP, feel free to use these alternatives.They will probably work fine for most use cases, and note that the abovepoints are not specific to Celery; If using Redis/database as a queue workedfine for you before, it probably will now. You can always upgrade laterif you need to... _faq-is-celery-multilingual:Is Celery multilingual?------------------------**Answer:** Yes.:mod:`~celery.bin.celeryd` is an implementation of Celery in python. If thelanguage has an AMQP client, there shouldn't be much work to create a workerin your language.  A Celery worker is just a program connecting to the brokerto process messages.Also, there's another way to be language indepedent, and that is to use RESTtasks, instead of your tasks being functions, they're URLs. With thisinformation you can even create simple web servers that enable preloading ofcode. See: `User Guide: Remote Tasks`_... _`User Guide: Remote Tasks`:    http://ask.github.com/celery/userguide/remote-tasks.html.. _faq-troubleshooting:Troubleshooting===============.. _faq-mysql-deadlocks:MySQL is throwing deadlock errors, what can I do?-------------------------------------------------**Answer:** MySQL has default isolation level set to ``REPEATABLE-READ``,if you don't really need that, set it to ``READ-COMMITTED``.You can do that by adding the following to your :file:`my.cnf`::    [mysqld]    transaction-isolation = READ-COMMITTEDFor more information about InnoDBs transaction model see `MySQL - The InnoDBTransaction Model and Locking`_ in the MySQL user manual.(Thanks to Honza Kral and Anton Tsigularov for this solution).. _`MySQL - The InnoDB Transaction Model and Locking`: http://dev.mysql.com/doc/refman/5.1/en/innodb-transaction-model.html.. _faq-worker-hanging:celeryd is not doing anything, just hanging--------------------------------------------**Answer:** See `MySQL is throwing deadlock errors, what can I do?`_.            or `Why is Task.delay/apply\* just hanging?`... _faq-publish-hanging:Why is Task.delay/apply\*/celeryd just hanging?-----------------------------------------------**Answer:** There is a bug in some AMQP clients that will make it hang ifit's not able to authenticate the current user, the password doesn't match orthe user does not have access to the virtual host specified. Be sure to checkyour broker logs (for RabbitMQ that is :file:`/var/log/rabbitmq/rabbit.log` onmost systems), it usually contains a message describing the reason... _faq-celeryd-on-freebsd:Why won't celeryd run on FreeBSD?---------------------------------**Answer:** multiprocessing.Pool requires a working POSIX semaphoreimplementation which isn't enabled in FreeBSD by default. You have to enablePOSIX semaphores in the kernel and manually recompile multiprocessing.Luckily, Viktor Petersson has written a tutorial to get you started withCelery on FreeBSD here:http://www.playingwithwire.com/2009/10/how-to-get-celeryd-to-work-on-freebsd/.. _faq-duplicate-key-errors:I'm having ``IntegrityError: Duplicate Key`` errors. Why?---------------------------------------------------------**Answer:** See `MySQL is throwing deadlock errors, what can I do?`_.Thanks to howsthedotcom... _faq-worker-stops-processing:Why aren't my tasks processed?------------------------------**Answer:** With RabbitMQ you can see how many consumers are currentlyreceiving tasks by running the following command::    $ rabbitmqctl list_queues -p <myvhost> name messages consumers    Listing queues ...    celery     2891    2This shows that there's 2891 messages waiting to be processed in the taskqueue, and there are two consumers processing them.One reason that the queue is never emptied could be that you have a staleworker process taking the messages hostage. This could happen if celerydwasn't properly shut down.When a message is recieved by a worker the broker waits for it to beacknowledged before marking the message as processed. The broker will notre-send that message to another consumer until the consumer is shut downproperly.If you hit this problem you have to kill all workers manually and restartthem::    ps auxww | grep celeryd | awk '{print $2}' | xargs killYou might have to wait a while until all workers have finished the work they'redoing. If it's still hanging after a long time you can kill them by forcewith::    ps auxww | grep celeryd | awk '{print $2}' | xargs kill -9.. _faq-task-does-not-run:Why won't my Task run?----------------------**Answer:** There might be syntax errors preventing the tasks module being imported.You can find out if Celery is able to run the task by executing thetask manually:    >>> from myapp.tasks import MyPeriodicTask    >>> MyPeriodicTask.delay()Watch celeryds logfile to see if it's able to find the task, or if someother error is happening... _faq-periodic-task-does-not-run:Why won't my Periodic Task run?-------------------------------**Answer:** See `Why won't my Task run?`_... _faq-purge-the-queue:How do I discard all waiting tasks?------------------------------------**Answer:** Use :func:`~celery.task.control.discard_all`, like this:    >>> from celery.task.control import discard_all    >>> discard_all()    1753The number 1753 is the number of messages deleted.You can also start :mod:`~celery.bin.celeryd` with the:option:`--discard` argument which will accomplish the same thing... _faq-messages-left-after-purge:I've discarded messages, but there are still messages left in the queue?------------------------------------------------------------------------**Answer:** Tasks are acknowledged (removed from the queue) as soonas they are actually executed. After the worker has received a task, it willtake some time until it is actually executed, especially if there are a lotof tasks already waiting for execution. Messages that are not acknowledged areheld on to by the worker until it closes the connection to the broker (AMQPserver). When that connection is closed (e.g because the worker was stopped)the tasks will be re-sent by the broker to the next available worker (or thesame worker when it has been restarted), so to properly purge the queue ofwaiting tasks you have to stop all the workers, and then discard the tasksusing :func:`~celery.task.control.discard_all`... _faq-results:Results=======.. _faq-get-result-by-task-id:How do I get the result of a task if I have the ID that points there?----------------------------------------------------------------------**Answer**: Use ``Task.AsyncResult``::    >>> result = MyTask.AsyncResult(task_id)    >>> result.get()This will give you a :class:`~celery.result.BaseAsyncResult` instanceusing the tasks current result backend.If you need to specify a custom result backend you should use:class:`celery.result.BaseAsyncResult` directly::    >>> from celery.result import BaseAsyncResult    >>> result = BaseAsyncResult(task_id, backend=...)    >>> result.get().. _faq-brokers:Brokers=======Why is RabbitMQ crashing?-------------------------RabbitMQ will crash if it runs out of memory. This will be fixed in afuture release of RabbitMQ. please refer to the RabbitMQ FAQ:http://www.rabbitmq.com/faq.html#node-runs-out-of-memory.. note::    This is no longer the case, RabbitMQ versions 2.0 and above    includes a new persister, that is tolerant to out of memory    errors. RabbitMQ 2.1 or higher is recommended for Celery.    If you're still running an older version of RabbitMQ and experience    crashes, then please upgrade!Some common Celery misconfigurations can eventually lead to a crashon older version of RabbitMQ. Even if it doesn't crash, thesemisconfigurations can still consume a lot of resources, so it is veryimportant that you are aware of them.* Events.Running :mod:`~celery.bin.celeryd` with the :option:`-E`/:option:`--events`option will send messages for events happening inside of the worker.Events should only be enabled if you have an active monitor consuming them,or if you purge the event queue periodically.* AMQP backend results.When running with the AMQP result backend, every task result will be sentas a message. If you don't collect these results, they will build up andRabbitMQ will eventually run out of memory.If you don't use the results for a task, make sure you set the``ignore_result`` option:.. code-block python    @task(ignore_result=True)    def mytask():        ...    class MyTask(Task):        ignore_result = TrueResults can also be disabled globally using the:setting:`CELERY_IGNORE_RESULT` setting... note::    Celery version 2.1 added support for automatic expiration of    AMQP result backend results.    To use this you need to run RabbitMQ 2.1 or higher and enable    the :setting:`CELERY_AMQP_TASK_RESULT_EXPIRES` setting... _faq-use-celery-with-stomp:Can I use Celery with ActiveMQ/STOMP?-------------------------------------**Answer**: Yes, but this is somewhat experimental for now.It is working ok in a test configuration, but it has notbeen tested in production. If you have any problemsusing STOMP with Celery, please report an issue here::    http://github.com/ask/celery/issues/The STOMP carrot backend requires the `stompy`_ library::    $ pip install stompy    $ cd python-stomp    $ sudo python setup.py install    $ cd .... _`stompy`: http://pypi.python.org/pypi/stompyIn this example we will use a queue called ``celery`` which we created inthe ActiveMQ web admin interface.**Note**: When using ActiveMQ the queue name needs to have ``"/queue/"``prepended to it. i.e. the queue ``celery`` becomes ``/queue/celery``.Since STOMP doesn't have exchanges and the routing capabilities of AMQP,you need to set ``exchange`` name to the same as the queue name. This isa minor inconvenience since carrot needs to maintain the same interfacefor both AMQP and STOMP.Use the following settings in your :file:`celeryconfig.py`/django :file:`settings.py`:.. code-block:: python    # Use the stomp carrot backend.    CARROT_BACKEND = "stomp"    # STOMP hostname and port settings.    BROKER_HOST = "localhost"    BROKER_PORT = 61613    # The queue name to use (the exchange *must* be set to the    # same as the queue name when using STOMP)    CELERY_DEFAULT_QUEUE = "/queue/celery"    CELERY_DEFAULT_EXCHANGE = "/queue/celery"     CELERY_QUEUES = {        "/queue/celery": {"exchange": "/queue/celery"}    }.. _faq-stomp-missing-features:What features are not supported when using ghettoq/STOMP?---------------------------------------------------------This is a (possible incomplete) list of features not available whenusing the STOMP backend:    * routing keys    * exchange types (direct, topic, headers, etc)    * immediate    * mandatory.. _faq-tasks:Tasks=====.. _faq-tasks-connection-reuse:How can I reuse the same connection when applying tasks?--------------------------------------------------------**Answer**: See :ref:`executing-connections`... _faq-execute-task-by-name:Can I execute a task by name?-----------------------------**Answer**: Yes. Use :func:`celery.execute.send_task`.You can also execute a task by name from any languagethat has an AMQP client.    >>> from celery.execute import send_task    >>> send_task("tasks.add", args=[2, 2], kwargs={})    <AsyncResult: 373550e8-b9a0-4666-bc61-ace01fa4f91d>.. _faq-get-current-task-id:How can I get the task id of the current task?----------------------------------------------**Answer**: Celery does set some default keyword arguments if the taskaccepts them (you can accept them by either using ``**kwargs``, or list themspecifically)::    @task    def mytask(task_id=None):        cache.set(task_id, "Running")The default keyword arguments are documented here:http://celeryq.org/docs/userguide/tasks.html#default-keyword-arguments.. _faq-custom-task-ids:Can I specify a custom task_id?-------------------------------**Answer**: Yes.  Use the ``task_id`` argument to:meth:`~celery.execute.apply_async`::    >>> task.apply_async(args, kwargs, task_id="...")Can I use decorators with tasks?--------------------------------**Answer**: Yes.  But please see note at :ref:`tasks-decorating`... _faq-natural-task-ids:Can I use natural task ids?---------------------------**Answer**: Yes, but make sure it is unique, as the behaviorfor two tasks existing with the same id is undefined.The world will probably not explode, but at the worstthey can overwrite each others results... _faq-task-callbacks:How can I run a task once another task has finished?----------------------------------------------------**Answer**: You can safely launch a task inside a task.Also, a common pattern is to use callback tasks:.. code-block:: python    @task()    def add(x, y, callback=None):        result = x + y        if callback:            subtask(callback).delay(result)        return result    @task(ignore_result=True)    def log_result(result, **kwargs):        logger = log_result.get_logger(**kwargs)        logger.info("log_result got: %s" % (result, ))Invocation::    >>> add.delay(2, 2, callback=log_result.subtask())See :doc:`userguide/tasksets` for more information... _faq-cancel-task:Can I cancel the execution of a task?-------------------------------------**Answer**: Yes. Use ``result.revoke``::    >>> result = add.apply_async(args=[2, 2], countdown=120)    >>> result.revoke()or if you only have the task id::    >>> from celery.task.control import revoke    >>> revoke(task_id).. _faq-node-not-receiving-broadcast-commands:Why aren't my remote control commands received by all workers?--------------------------------------------------------------**Answer**: To receive broadcast remote control commands, every worker nodeuses its hostname to create a unique queue name to listen to,so if you have more than one worker with the same hostname, thecontrol commands will be recieved in round-robin between them.To work around this you can explicitly set the hostname for every workerusing the :option:`--hostname` argument to :mod:`~celery.bin.celeryd`::    $ celeryd --hostname=$(hostname).1    $ celeryd --hostname=$(hostname).2etc, etc... _faq-task-routing:Can I send some tasks to only some servers?--------------------------------------------**Answer:** Yes. You can route tasks to an arbitrary server using AMQP,and a worker can bind to as many queues as it wants.See :doc:`userguide/routing` for more information... _faq-change-periodic-task-interval-at-runtime:Can I change the interval of a periodic task at runtime?--------------------------------------------------------**Answer**: Yes. You can override ``PeriodicTask.is_due`` or turn``PeriodicTask.run_every`` into a property:.. code-block:: python    class MyPeriodic(PeriodicTask):        def run(self):            # ...        @property        def run_every(self):            return get_interval_from_database(...).. _faq-task-priorities:Does celery support task priorities?------------------------------------**Answer**: No. In theory, yes, as AMQP supports priorities. HoweverRabbitMQ doesn't implement them yet.The usual way to prioritize work in Celery, is to route high priority tasksto different servers. In the real world this may actually work better than per messagepriorities. You can use this in combination with rate limiting to achieve ahighly performant system... _faq-acks_late-vs-retry:Should I use retry or acks_late?--------------------------------**Answer**: Depends. It's not necessarily one or the other, you may wantto use both.``Task.retry`` is used to retry tasks, notably for expected errors thatis catchable with the ``try:`` block. The AMQP transaction is not usedfor these errors: **if the task raises an exception it is still acked!**.The ``acks_late`` setting would be used when you need the task to beexecuted again if the worker (for some reason) crashes mid-execution.It's important to note that the worker is not known to crash, and ifit does it is usually an unrecoverable error that requires humanintervention (bug in the worker, or task code).In an ideal world you could safely retry any task that has failed, butthis is rarely the case. Imagine the following task:.. code-block:: python    @task()    def process_upload(filename, tmpfile):        # Increment a file count stored in a database        increment_file_counter()        add_file_metadata_to_db(filename, tmpfile)        copy_file_to_destination(filename, tmpfile)If this crashed in the middle of copying the file to its destinationthe world would contain incomplete state. This is not a criticalscenario of course, but you can probably imagine something far moresinister. So for ease of programming we have less reliability;It's a good default, users who require it and know what theyare doing can still enable acks_late (and in the future hopefullyuse manual acknowledgement)In addition ``Task.retry`` has features not available in AMQPtransactions: delay between retries, max retries, etc.So use retry for Python errors, and if your task is reentrantcombine that with ``acks_late`` if that level of reliabilityis required... _faq-schedule-at-specific-time:Can I schedule tasks to execute at a specific time?---------------------------------------------------.. module:: celery.task.base**Answer**: Yes. You can use the ``eta`` argument of :meth:`Task.apply_async`.Or to schedule a periodic task at a specific time, use the:class:`celery.task.schedules.crontab` schedule behavior:.. code-block:: python    from celery.task.schedules import crontab    from celery.decorators import periodic_task    @periodic_task(run_every=crontab(hours=7, minute=30, day_of_week="mon"))    def every_monday_morning():        print("This is run every monday morning at 7:30").. _faq-safe-worker-shutdown:How do I shut down ``celeryd`` safely?--------------------------------------**Answer**: Use the :sig:`TERM` signal, and the worker will finish all currentlyexecuting jobs and shut down as soon as possible. No tasks should be lost.You should never stop :mod:`~celery.bin.celeryd` with the :sig:`KILL` signal(:option:`-9`), unless you've tried :sig:`TERM` a few times and waited a fewminutes to let it get a chance to shut down.  As if you do tasks may beterminated mid-execution, and they will not be re-run unless you have the``acks_late`` option set (``Task.acks_late`` / :setting:`CELERY_ACKS_LATE`)... seealso::    :ref:`worker-stopping`.. _faq-daemonizing:How do I run celeryd in the background on [platform]?-----------------------------------------------------**Answer**: Please see :ref:`daemonizing`... _faq-windows:Windows=======.. _faq-windows-worker-spawn-loop:celeryd keeps spawning processes at startup-------------------------------------------**Answer**: This is a known issue on Windows.You have to start celeryd with the command::    $ python -m celeryd.bin.celerydAny additional arguments can be appended to this command.See http://bit.ly/bo9RSw.. _faq-windows-worker-embedded-beat:The ``-B`` / ``--beat`` option to celeryd doesn't work?----------------------------------------------------------------**Answer**: That's right. Run ``celerybeat`` and ``celeryd`` as separateservices instead... _faq-windows-django-settings:``django-celery`` can’t find settings?--------------------------------------**Answer**: You need to specify the :option:`--settings` argument to:program:`manage.py`::    $ python manage.py celeryd start --settings=settingsSee http://bit.ly/bo9RSw
 |