app-overview.rst 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. =============================
  2. "The Big Instance" Refactor
  3. =============================
  4. The `app` branch is a work-in-progress to remove
  5. the use of a global configuration in Celery.
  6. Celery can now be instantiated, which means several
  7. instances of Celery may exist in the same process space.
  8. Also, large parts can be customized without resorting to monkey
  9. patching.
  10. Examples
  11. ========
  12. Creating a Celery instance::
  13. >>> from celery import Celery
  14. >>> celery = Celery()
  15. >>> celery.config_from_object("celeryconfig")
  16. >>> celery.config.from_envvar("CELERY_CONFIG_MODULE")
  17. Creating tasks:
  18. .. code-block:: python
  19. @celery.task()
  20. def add(x, y):
  21. return x + y
  22. Creating custom Task subclasses:
  23. .. code-block:: python
  24. Task = celery.create_task_cls()
  25. class DebugTask(Task):
  26. abstract = True
  27. def on_failure(self, \*args, \*\*kwargs):
  28. import pdb
  29. pdb.set_trace()
  30. @celery.task(base=DebugTask)
  31. def add(x, y):
  32. return x + y
  33. Starting a worker:
  34. .. code-block:: python
  35. worker = celery.Worker(loglevel="INFO")
  36. Getting access to the configuration::
  37. .. code-block:: python
  38. celery.conf.CELERY_ALWAYS_EAGER = True
  39. celery.conf["CELERY_ALWAYS_EAGER"] = True
  40. Controlling workerse
  41. >>> celery.control.inspect().active()
  42. >>> celery.control.rate_limit(add.name, "100/m")
  43. >>> celery.control.broadcast("shutdown")
  44. >>> celery.control.discard_all()
  45. Other interesting attributes::
  46. # Establish broker connection.
  47. >>> celery.broker_connection()
  48. # AMQP Specific features.
  49. >>> celery.amqp
  50. >>> celery.amqp.Router
  51. >>> celery.amqp.get_queues()
  52. >>> celery.amqp.get_consumer_set()
  53. # Loader
  54. >>> celery.loader
  55. # Default backend
  56. >>> celery.backend
  57. As you can probably see, this really opens up another
  58. dimension of customization abilities.
  59. Aliases (Pending deprecation)
  60. =============================
  61. * celery.task.base
  62. * .Task -> {app.create_task_cls}
  63. * celery.task.sets
  64. * .TaskSet -> {app.TaskSet}
  65. * celery.decorators
  66. * .task -> {app.task}
  67. * celery.execute
  68. * .apply_async -> {task.apply_async}
  69. * .apply -> {task.apply}
  70. * .send_task -> {app.send_task}
  71. * .delay_task -> no alternative
  72. * celery.log
  73. * .get_default_logger -> {app.log.get_default_logger}
  74. * .setup_logger -> {app.log.setup_logger}
  75. * .get_task_logger -> {app.log.get_task_logger}
  76. * .setup_task_logger -> {app.log.setup_task_logger}
  77. * .setup_logging_subsystem -> {app.log.setup_logging_subsystem}
  78. * .redirect_stdouts_to_logger -> {app.log.redirect_stdouts_to_logger}
  79. * celery.messaging
  80. * .establish_connection -> {app.broker_connection}
  81. * .with_connection -> {app.with_connection}
  82. * .get_consumer_set -> {app.amqp.get_consumer_set}
  83. * .TaskPublisher -> {app.amqp.TaskPublisher}
  84. * .TaskConsumer -> {app.amqp.TaskConsumer}
  85. * .ConsumerSet -> {app.amqp.ConsumerSet}
  86. * celery.conf.* -> {app.conf}
  87. **NOTE**: All configuration keys are now named the same
  88. as in the configuration. So the key "CELERY_ALWAYS_EAGER"
  89. is accessed as::
  90. >>> app.conf.CELERY_ALWAYS_EAGER
  91. instead of::
  92. >>> from celery import conf
  93. >>> conf.ALWAYS_EAGER
  94. * .get_queues -> {app.amqp.get_queues}
  95. * celery.task.control
  96. * .broadcast -> {app.control.broadcast}
  97. * .rate_limit -> {app.control.rate_limit}
  98. * .ping -> {app.control.ping}
  99. * .revoke -> {app.control.revoke}
  100. * .discard_all -> {app.control.discard_all}
  101. * .inspect -> {app.control.inspect}
  102. * celery.utils.info
  103. * .humanize_seconds -> celery.utils.timeutils.humanize_seconds
  104. * .textindent -> celery.utils.textindent
  105. * .get_broker_info -> {app.amqp.get_broker_info}
  106. * .format_broker_info -> {app.amqp.format_broker_info}
  107. * .format_queues -> {app.amqp.format_queues}
  108. Default App Usage
  109. =================
  110. To be backward compatible, it must be possible
  111. to use all the classes/functions without passing
  112. an explicit app instance.
  113. This is achieved by having all app-dependent objects
  114. use :data:`~celery.app.default_app` if the app instance
  115. is missing.
  116. .. code-block:: python
  117. from celery.app import app_or_default
  118. class SomeClass(object):
  119. def __init__(self, app=None):
  120. self.app = app_or_default(app)
  121. The problem with this approach is that there is a chance
  122. that the app instance is lost along the way, and everything
  123. seems to be working normally. Testing app instance leaks
  124. is hard. The environment variable :envvar:`CELERY_TRACE_APP`
  125. can be used, when this is enabled :func:`celery.app.app_or_default`
  126. will raise an exception whenever it has to go back to the default app
  127. instance.
  128. App Dependency Tree
  129. -------------------
  130. * {app}
  131. * celery.loaders.base.BaseLoader
  132. * celery.backends.base.BaseBackend
  133. * {app.TaskSet}
  134. * celery.task.sets.TaskSet (app.TaskSet)
  135. * [app.TaskSetResult]
  136. * celery.result.TaskSetResult (app.TaskSetResult)
  137. * {app.AsyncResult}
  138. * celery.result.BaseAsyncResult / celery.result.AsyncResult
  139. * celery.bin.celeryd.WorkerCommand
  140. * celery.apps.worker.Worker
  141. * celery.worker.WorkerController
  142. * celery.worker.listener.CarrotListener
  143. * celery.worker.job.TaskRequest
  144. * celery.events.EventDispatcher
  145. * celery.worker.control.ControlDispatch
  146. * celery.woker.control.registry.Panel
  147. * celery.pidbox.BroadcastPublisher
  148. * celery.pidbox.BroadcastConsumer
  149. * celery.worker.controllers.Mediator
  150. * celery.beat.EmbeddedService
  151. * celery.bin.celeryev.run_celeryev
  152. * celery.events.snapshot.evcam
  153. * celery.events.snapshot.Polaroid
  154. * celery.events.EventReceiver
  155. * celery.events.cursesmon.evtop
  156. * celery.events.EventReceiver
  157. * celery.events.cursesmon.CursesMonitor
  158. * celery.events.dumper
  159. * celery.events.EventReceiver
  160. * celery.bin.celeryctl.celeryctl
  161. * celery.bin.celeryctl.Command
  162. * celery.bin.caqmadm.AMQPAdmin
  163. * celery.bin.celerybeat.BeatCommand
  164. * celery.apps.beat.Beat
  165. * celery.beat.Service
  166. * celery.beat.Scheduler