README.rst 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. ============================================
  2. celery - Distributed Task Queue for Django.
  3. ============================================
  4. :Version: 0.2.4
  5. Introduction
  6. ============
  7. ``celery`` is a distributed task queue framework for Django.
  8. It is used for executing tasks *asynchronously*, routed to one or more
  9. worker servers, running concurrently using multiprocessing.
  10. It is designed to solve certain problems related to running websites
  11. demanding high-availability and performance.
  12. It is perfect for filling caches, posting updates to twitter, mass
  13. downloading data like syndication feeds or web scraping. Use-cases are
  14. plentiful. Implementing these features asynchronously using ``celery`` is
  15. easy and fun, and the performance improvements can make it more than
  16. worthwhile.
  17. Features
  18. ========
  19. * Uses AMQP messaging (RabbitMQ, ZeroMQ) to route tasks to the
  20. worker servers.
  21. * You can run as many worker servers as you want, and still
  22. be *guaranteed that the task is only executed once.*
  23. * Tasks are executed *concurrently* using the Python 2.6
  24. ``multiprocessing`` module (also available as a back-port
  25. to older python versions)
  26. * Supports *periodic tasks*, which makes it a (better) replacement
  27. for cronjobs.
  28. * When a task has been executed, the return value is stored using either
  29. a MySQL/Oracle/PostgreSQL/SQLite database, memcached,
  30. or Tokyo Tyrant back-end.
  31. * If the task raises an exception, the exception instance is stored,
  32. instead of the return value.
  33. * All tasks has a Universally Unique Identifier (UUID), which is the
  34. task id, used for querying task status and return values.
  35. * Supports *task-sets*, which is a task consisting of several sub-tasks.
  36. You can find out how many, or if all of the sub-tasks has been executed.
  37. Excellent for progress-bar like functionality.
  38. * Has a ``map`` like function that uses tasks, called ``dmap``.
  39. * However, you rarely want to wait for these results in a web-environment.
  40. You'd rather want to use Ajax to poll the task status, which is
  41. available from a URL like ``celery/<task_id>/status/``. This view
  42. returns a JSON-serialized data structure containing the task status,
  43. and the return value if completed, or exception on failure.
  44. API Reference Documentation
  45. ===========================
  46. The `API Reference Documentation`_ is hosted at Github
  47. (http://ask.github.com/celery)
  48. .. _`API Reference Docmentation`: http://ask.github.com/celery/
  49. Installation
  50. =============
  51. You can install ``celery`` either via the Python Package Index (PyPI)
  52. or from source.
  53. To install using ``pip``,::
  54. $ pip install celery
  55. To install using ``easy_install``,::
  56. $ easy_install celery
  57. If you have downloaded a source tarball you can install it
  58. by doing the following,::
  59. $ python setup.py build
  60. # python setup.py install # as root
  61. Usage
  62. =====
  63. Have to write a cool tutorial, but here is some simple usage info.
  64. *Note* You need to have a AMQP message broker running, like `RabbitMQ`_,
  65. and you need to have the amqp server setup in your settings file, as described
  66. in the `carrot distribution README`_.
  67. *Note* If you're running ``SQLite`` as the database back-end, ``celeryd`` will
  68. only be able to process one message at a time, this is because ``SQLite``
  69. doesn't allow concurrent writes.
  70. .. _`RabbitMQ`: http://www.rabbitmq.com
  71. .. _`carrot distribution README`: http://pypi.python.org/pypi/carrot/0.3.3
  72. Defining tasks
  73. --------------
  74. >>> from celery.task import tasks
  75. >>> from celery.log import setup_logger
  76. >>> def do_something(some_arg, **kwargs):
  77. ... logger = setup_logger(**kwargs)
  78. ... logger.info("Did something: %s" % some_arg)
  79. ... return 42
  80. >>> task.register(do_something, "do_something")
  81. Tell the celery daemon to run a task
  82. -------------------------------------
  83. >>> from celery.task import delay_task
  84. >>> delay_task("do_something", some_arg="foo bar baz")
  85. Execute a task, and get its return value.
  86. -----------------------------------------
  87. >>> from celery.task import delay_task
  88. >>> result = delay_task("do_something", some_arg="foo bar baz")
  89. >>> result.ready()
  90. False
  91. >>> result.get() # Waits until the task is done.
  92. 42
  93. >>> result.status()
  94. 'DONE'
  95. If the task raises an exception, the tasks status will be ``FAILURE``, and
  96. ``result.result`` will contain the exception instance raised.
  97. Running the celery daemon
  98. --------------------------
  99. ::
  100. $ cd mydjangoproject
  101. $ env DJANGO_SETTINGS_MODULE=settings celeryd
  102. [....]
  103. [2009-04-23 17:44:05,115: INFO/Process-1] Did something: foo bar baz
  104. [2009-04-23 17:44:05,118: INFO/MainProcess] Waiting for queue.
  105. Auto-discovery of tasks
  106. -----------------------
  107. ``celery`` has an auto-discovery feature like the Django Admin, that
  108. automatically loads any ``tasks.py`` module in the applications listed
  109. in ``settings.INSTALLED_APPS``.
  110. A good place to add this command could be in your ``urls.py``,
  111. ::
  112. from celery.task import tasks
  113. tasks.autodiscover()
  114. Then you can add new tasks in your applications ``tasks.py`` module,
  115. ::
  116. from celery.task import tasks
  117. from celery.log import setup_logger
  118. from clickcounter.models import ClickCount
  119. def increment_click(for_url, **kwargs):
  120. logger = setup_logger(**kwargs)
  121. clicks_for_url, cr = ClickCount.objects.get_or_create(url=for_url)
  122. clicks_for_url.clicks = clicks_for_url.clicks + 1
  123. clicks_for_url.save()
  124. logger.info("Incremented click count for %s (not at %d)" % (
  125. for_url, clicks_for_url.clicks)
  126. tasks.register(increment_click, "increment_click")
  127. Periodic Tasks
  128. ---------------
  129. Periodic tasks are tasks that are run every ``n`` seconds. They don't
  130. support extra arguments. Here's an example of a periodic task:
  131. >>> from celery.task import tasks, PeriodicTask
  132. >>> from datetime import timedelta
  133. >>> class MyPeriodicTask(PeriodicTask):
  134. ... name = "foo.my-periodic-task"
  135. ... run_every = timedelta(seconds=30)
  136. ...
  137. ... def run(self, **kwargs):
  138. ... logger = self.get_logger(**kwargs)
  139. ... logger.info("Running periodic task!")
  140. ...
  141. >>> tasks.register(MyPeriodicTask)
  142. For periodic tasks to work you need to add ``celery`` to ``INSTALLED_APPS``,
  143. and issue a ``syncdb``.
  144. License
  145. =======
  146. This software is licensed under the ``New BSD License``. See the ``LICENSE``
  147. file in the top distribution directory for the full license text.
  148. .. # vim: syntax=rst expandtab tabstop=4 shiftwidth=4 shiftround