app-overview.rst 6.5 KB

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