README.rst 4.0 KB

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