application.rst 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. .. _guide-app:
  2. =============
  3. Application
  4. =============
  5. The Celery library must instantiated before use, and this instance
  6. is called the application.
  7. Multiple Celery applications with different configuration
  8. and components can co-exist in the same process space,
  9. but there is always an app exposed as the 'current app' in each
  10. thread.
  11. Configuration
  12. =============
  13. There are lots of different options you can set that will change how
  14. Celery work. These options can be set on the app instance directly,
  15. or you can use a dedicated configuration module.
  16. The current configuration is always available as :attr:`@Celery.conf`::
  17. >>> celery.conf.CELERY_TIMEZONE
  18. "Europe/London"
  19. The configuration actually consists of multiple dictionaries
  20. that are consulted in order:
  21. #. Changes made at runtime.
  22. #. Any configuration module (``loader.conf``).
  23. #. The default configuration (:mod:`celery.app.defaults`).
  24. When the app is serialized
  25. only the configuration changes will be transferred.
  26. .. topic:: The "default app".
  27. Celery did not always work this way, it used to be that
  28. there was only a module-based API, and for backwards compatibility
  29. the old API is still there.
  30. Celery always creates a special app that is the "default app",
  31. and this is used if no custom application has been instantiated.
  32. The :mod:`celery.task` module is there to accommodate the old API,
  33. and should not be used if you use a custom app. You should
  34. always use the methods on the app instance, not the module based API.
  35. For example, the old Task base class enables many compatibility
  36. features where some may be incompatible with newer features, such
  37. as task methods:
  38. .. code-block:: python
  39. from celery.task import Task # << OLD Task base class.
  40. from celery import Task # << NEW base class.
  41. The new base class is recommended even if you use the old
  42. module-based API.
  43. .. topic:: Evolving the API
  44. Celery has changed a lot in the 3 years since it was initially
  45. created.
  46. For example, in the beginning it was possible to use any callable as
  47. a task::
  48. .. code-block:: python
  49. def hello(to):
  50. return "hello %s" % to
  51. >>> from celery.execute import apply_async
  52. >>> apply_async(hello, ("world!", ))
  53. or you could also create a ``Task`` class to set
  54. certain options, or override other behavior
  55. .. code-block:: python
  56. from celery.task import Task
  57. from celery.registry import tasks
  58. class Hello(Task):
  59. send_error_emails = True
  60. def run(self, to):
  61. return "hello %s" % to
  62. tasks.register(Hello)
  63. >>> Hello.delay("world!")
  64. Later, it was decided that passing arbitrary call-ables
  65. was an anti-pattern, since it makes it very hard to use
  66. serializers other than pickle, and the feature was removed
  67. in 2.0, replaced by task decorators::
  68. .. code-block:: python
  69. from celery.task import task
  70. @task(send_error_emails=True)
  71. def hello(x):
  72. return "hello %s" % to