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