guide.rst 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. .. _internals-guide:
  2. ================================
  3. Contributors Guide to the Code
  4. ================================
  5. .. contents::
  6. :local:
  7. Applications vs. "single mode"
  8. ==============================
  9. In the beginning Celery was developed for Django, simply because
  10. this enabled us get the project started quickly, while also having
  11. a large potential user base.
  12. In Django there is a global settings object, so multiple Django projects
  13. can't co-exist in the same process space, this later posed a problem
  14. for using Celery with frameworks that doesn't have this limitation.
  15. Therefore the app concept was introduced. When using apps you use 'celery'
  16. objects instead of importing things from celery submodules, this sadly
  17. also means that Celery essentially has two APIs.
  18. Here's an example using Celery in single-mode:
  19. .. code-block:: python
  20. from celery.task import task
  21. from celery.task.control import inspect
  22. from .models import CeleryStats
  23. @task
  24. def write_stats_to_db():
  25. stats = inspect().stats(timeout=1)
  26. for node_name, reply in stats:
  27. CeleryStats.objects.update_stat(node_name, stats)
  28. and here's the same using Celery app objects:
  29. .. code-block:: python
  30. from .celery import celery
  31. from .models import CeleryStats
  32. @celery.task
  33. def write_stats_to_db():
  34. stats = celery.control.inspect().stats(timeout=1)
  35. for node_name, reply in stats:
  36. CeleryStats.objects.update_stat(node_name, stats)
  37. In the example above the actual application instance is imported
  38. from a module in the project, this module could look something like this:
  39. .. code-block:: python
  40. from celery import Celery
  41. celery = Celery()
  42. celery.config_from_object(BROKER_URL="amqp://")
  43. Module Overview
  44. ===============
  45. - celery.app
  46. This is the core of Celery: the entry-point for all functionality.
  47. - celery.loaders
  48. Every app must have a loader. The loader decides how configuration
  49. is read, what happens when the worker starts, when a task starts and ends,
  50. and so on.
  51. The loaders included are:
  52. - app
  53. Custom celery app instances uses this loader by default.
  54. - default
  55. "single-mode" uses this loader by default.
  56. Extension loaders also exist, like ``django-celery``, ``celery-pylons``
  57. and so on.
  58. - celery.worker
  59. This is the worker implementation.
  60. - celery.backends
  61. Task result backends live here.
  62. - celery.apps
  63. Major user applications: ``celeryd``, and ``celerybeat``
  64. - celery.bin
  65. Command line applications.
  66. setup.py creates setuptools entrypoints for these.
  67. - celery.concurrency
  68. Execution pool implementations (processes, eventlet, gevent, threads).
  69. - celery.db
  70. Database models for the SQLAlchemy database result backend.
  71. (should be moved into :mod:`celery.backends.database`)
  72. - celery.events
  73. Sending and consuming monitoring events, also includes curses monitor,
  74. event dumper and utilities to work with in-memory cluster state.
  75. - celery.execute.trace
  76. How tasks are executed and traced by the worker, and in eager mode.
  77. - celery.security
  78. Security related functionality, currently a serializer using
  79. cryptographic digests.
  80. - celery.task
  81. single-mode interface to creating tasks, and controlling workers.
  82. - celery.tests
  83. The celery unittest suite.
  84. - celery.utils
  85. Utility functions used by the celery code base.
  86. Much of it is there to be compatible across Python versions.
  87. - celery.contrib
  88. Additional public code that doesn't fit into any other namespace.