README.rst 6.5 KB

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