README.rst 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. ============================================
  2. celery - Distributed Task Queue for Django.
  3. ============================================
  4. :Authors:
  5. Ask Solem (askh@opera.com)
  6. :Version: 0.1.5
  7. Introduction
  8. ------------
  9. ``celery`` is a distributed task queue framework for Django.
  10. More information will follow.
  11. Installation
  12. =============
  13. You can install ``celery`` either via the Python Package Index (PyPI)
  14. or from source.
  15. To install using ``pip``,::
  16. $ pip install celery
  17. To install using ``easy_install``,::
  18. $ easy_install celery
  19. If you have downloaded a source tarball you can install it
  20. by doing the following,::
  21. $ python setup.py build
  22. # python setup.py install # as root
  23. Usage
  24. =====
  25. Have to write a cool tutorial, but here is some simple usage info.
  26. *Note* You need to have a AMQP message broker running, like `RabbitMQ`_,
  27. and you need to have the amqp server setup in your settings file, as described
  28. in the `carrot distribution README`_.
  29. *Note* If you're running ``SQLite`` as the database backend, ``celeryd`` will
  30. only be able to process one message at a time, this because ``SQLite`` doesn't
  31. allow concurrent writes.
  32. .. _`RabbitMQ`: http://www.rabbitmq.com
  33. .. _`carrot distribution README`: http://pypi.python.org/pypi/carrot/0.3.3
  34. Defining tasks
  35. --------------
  36. >>> from celery.task import tasks
  37. >>> from celery.log import setup_logger
  38. >>> def do_something(some_arg, **kwargs):
  39. ... logger = setup_logger(**kwargs)
  40. ... logger.info("Did something: %s" % some_arg)
  41. >>> task.register(do_something, "do_something")
  42. *Note* Task functions only supports keyword arguments.
  43. Tell the celery daemon to run a task
  44. -------------------------------------
  45. >>> from celery.task import delay_task
  46. >>> delay_task("do_something", some_arg="foo bar baz")
  47. Running the celery daemon
  48. --------------------------
  49. ::
  50. $ cd mydjangoproject
  51. $ env DJANGO_SETTINGS_MODULE=settings celeryd
  52. [....]
  53. [2009-04-23 17:44:05,115: INFO/Process-1] Did something: foo bar baz
  54. [2009-04-23 17:44:05,118: INFO/MainProcess] Waiting for queue.
  55. Autodiscovery of tasks
  56. -----------------------
  57. ``celery`` has an autodiscovery feature like the Django Admin, that
  58. automatically loads any ``tasks.py`` module in the applications listed
  59. in ``settings.INSTALLED_APPS``.
  60. A good place to add this command could be in your ``urls.py``,
  61. ::
  62. from celery.task import tasks
  63. tasks.autodiscover()
  64. Then you can add new tasks in your applications ``tasks.py`` module,
  65. ::
  66. from celery.task import tasks
  67. from celery.log import setup_logger
  68. from clickcounter.models import ClickCount
  69. def increment_click(for_url, **kwargs):
  70. logger = setup_logger(**kwargs)
  71. clicks_for_url, cr = ClickCount.objects.get_or_create(url=for_url)
  72. clicks_for_url.clicks = clicks_for_url.clicks + 1
  73. clicks_for_url.save()
  74. logger.info("Incremented click count for %s (not at %d)" % (
  75. for_url, clicks_for_url.clicks)
  76. tasks.register(increment_click, "increment_click")
  77. Periodic Tasks
  78. ---------------
  79. Periodic tasks are tasks that are run every ``n`` seconds. They don't
  80. support extra arguments. Here's an example of a periodic task:
  81. >>> from celery.task import tasks, PeriodicTask
  82. >>> from datetime import timedelta
  83. >>> class MyPeriodicTask(PeriodicTask):
  84. ... name = "foo.my-periodic-task"
  85. ... run_every = timedelta(seconds=30)
  86. ...
  87. ... def run(self, **kwargs):
  88. ... logger = self.get_logger(**kwargs)
  89. ... logger.info("Running periodic task!")
  90. ...
  91. >>> tasks.register(MyPeriodicTask)
  92. For periodic tasks to work you need to add ``celery`` to ``INSTALLED_APPS``,
  93. and issue a ``syncdb``.
  94. License
  95. =======
  96. This software is licensed under the ``New BSD License``. See the ``LICENSE``
  97. file in the top distribution directory for the full license text.
  98. .. # vim: syntax=rst expandtab tabstop=4 shiftwidth=4 shiftround