app-overview.rst 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. >>> app = Celery()
  15. >>> app.config_from_object("celeryconfig")
  16. >>> #app.config_from_envvar("CELERY_CONFIG_MODULE")
  17. Creating tasks:
  18. .. code-block:: python
  19. @app.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. @app.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.task_always_eager = True
  39. celery.conf["task_always_eager"] = True
  40. Controlling workers::
  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_task_consumer()
  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. Deprecations
  60. ============
  61. * celery.task.ping
  62. celery.task.PingTask
  63. Inferior to the ping remote control command.
  64. Will be removed in Celery 2.3.
  65. Aliases (Pending deprecation)
  66. =============================
  67. * celery.task.base
  68. * .Task -> {app.Task / :class:`celery.app.task.Task`}
  69. * celery.task.sets
  70. * .TaskSet -> {app.TaskSet}
  71. * celery.decorators / celery.task
  72. * .task -> {app.task}
  73. * celery.execute
  74. * .apply_async -> {task.apply_async}
  75. * .apply -> {task.apply}
  76. * .send_task -> {app.send_task}
  77. * .delay_task -> no alternative
  78. * celery.log
  79. * .get_default_logger -> {app.log.get_default_logger}
  80. * .setup_logger -> {app.log.setup_logger}
  81. * .get_task_logger -> {app.log.get_task_logger}
  82. * .setup_task_logger -> {app.log.setup_task_logger}
  83. * .setup_logging_subsystem -> {app.log.setup_logging_subsystem}
  84. * .redirect_stdouts_to_logger -> {app.log.redirect_stdouts_to_logger}
  85. * celery.messaging
  86. * .establish_connection -> {app.broker_connection}
  87. * .with_connection -> {app.with_connection}
  88. * .get_consumer_set -> {app.amqp.get_task_consumer}
  89. * .TaskPublisher -> {app.amqp.TaskPublisher}
  90. * .TaskConsumer -> {app.amqp.TaskConsumer}
  91. * .ConsumerSet -> {app.amqp.ConsumerSet}
  92. * celery.conf.* -> {app.conf}
  93. **NOTE**: All configuration keys are now named the same
  94. as in the configuration. So the key "task_always_eager"
  95. is accessed as::
  96. >>> app.conf.task_always_eager
  97. instead of::
  98. >>> from celery import conf
  99. >>> conf.always_eager
  100. * .get_queues -> {app.amqp.get_queues}
  101. * celery.task.control
  102. * .broadcast -> {app.control.broadcast}
  103. * .rate_limit -> {app.control.rate_limit}
  104. * .ping -> {app.control.ping}
  105. * .revoke -> {app.control.revoke}
  106. * .discard_all -> {app.control.discard_all}
  107. * .inspect -> {app.control.inspect}
  108. * celery.utils.info
  109. * .humanize_seconds -> celery.utils.timeutils.humanize_seconds
  110. * .textindent -> celery.utils.textindent
  111. * .get_broker_info -> {app.amqp.get_broker_info}
  112. * .format_broker_info -> {app.amqp.format_broker_info}
  113. * .format_queues -> {app.amqp.format_queues}
  114. Default App Usage
  115. =================
  116. To be backward compatible, it must be possible
  117. to use all the classes/functions without passing
  118. an explicit app instance.
  119. This is achieved by having all app-dependent objects
  120. use :data:`~celery.app.default_app` if the app instance
  121. is missing.
  122. .. code-block:: python
  123. from celery.app import app_or_default
  124. class SomeClass(object):
  125. def __init__(self, app=None):
  126. self.app = app_or_default(app)
  127. The problem with this approach is that there is a chance
  128. that the app instance is lost along the way, and everything
  129. seems to be working normally. Testing app instance leaks
  130. is hard. The environment variable :envvar:`CELERY_TRACE_APP`
  131. can be used, when this is enabled :func:`celery.app.app_or_default`
  132. will raise an exception whenever it has to go back to the default app
  133. instance.
  134. App Dependency Tree
  135. -------------------
  136. * {app}
  137. * celery.loaders.base.BaseLoader
  138. * celery.backends.base.BaseBackend
  139. * {app.TaskSet}
  140. * celery.task.sets.TaskSet (app.TaskSet)
  141. * [app.TaskSetResult]
  142. * celery.result.TaskSetResult (app.TaskSetResult)
  143. * {app.AsyncResult}
  144. * celery.result.BaseAsyncResult / celery.result.AsyncResult
  145. * celery.bin.worker.WorkerCommand
  146. * celery.apps.worker.Worker
  147. * celery.worker.WorkerController
  148. * celery.worker.consumer.Consumer
  149. * celery.worker.request.Request
  150. * celery.events.EventDispatcher
  151. * celery.worker.control.ControlDispatch
  152. * celery.woker.control.registry.Panel
  153. * celery.pidbox.BroadcastPublisher
  154. * celery.pidbox.BroadcastConsumer
  155. * celery.worker.controllers.Mediator
  156. * celery.beat.EmbeddedService
  157. * celery.bin.events.EvCommand
  158. * celery.events.snapshot.evcam
  159. * celery.events.snapshot.Polaroid
  160. * celery.events.EventReceiver
  161. * celery.events.cursesmon.evtop
  162. * celery.events.EventReceiver
  163. * celery.events.cursesmon.CursesMonitor
  164. * celery.events.dumper
  165. * celery.events.EventReceiver
  166. * celery.bin.amqp.AMQPAdmin
  167. * celery.bin.beat.BeatCommand
  168. * celery.apps.beat.Beat
  169. * celery.beat.Service
  170. * celery.beat.Scheduler