__init__.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # -*- coding: utf-8 -*-
  2. """
  3. celery.app
  4. ~~~~~~~~~~
  5. Celery Application.
  6. """
  7. from __future__ import absolute_import, print_function, unicode_literals
  8. import os
  9. from celery.local import Proxy
  10. from celery import _state
  11. from celery._state import (
  12. get_current_app as current_app,
  13. get_current_task as current_task,
  14. connect_on_app_finalize, set_default_app, _get_active_apps, _task_stack,
  15. )
  16. from .base import Celery, AppPickler
  17. __all__ = ['Celery', 'AppPickler', 'default_app', 'app_or_default',
  18. 'bugreport', 'enable_trace', 'disable_trace', 'shared_task',
  19. 'set_default_app', 'current_app', 'current_task',
  20. 'push_current_task', 'pop_current_task']
  21. #: Proxy always returning the app set as default.
  22. default_app = Proxy(lambda: _state.default_app)
  23. #: Function returning the app provided or the default app if none.
  24. #:
  25. #: The environment variable :envvar:`CELERY_TRACE_APP` is used to
  26. #: trace app leaks. When enabled an exception is raised if there
  27. #: is no active app.
  28. app_or_default = None
  29. #: The 'default' loader is the default loader used by old applications.
  30. #: This is deprecated and should no longer be used as it's set too early
  31. #: to be affected by --loader argument.
  32. default_loader = os.environ.get('CELERY_LOADER') or 'default' # XXX
  33. #: Function used to push a task to the thread local stack
  34. #: keeping track of the currently executing task.
  35. #: You must remember to pop the task after.
  36. push_current_task = _task_stack.push
  37. #: Function used to pop a task from the thread local stack
  38. #: keeping track of the currently executing task.
  39. pop_current_task = _task_stack.pop
  40. def bugreport(app=None):
  41. return (app or current_app()).bugreport()
  42. def _app_or_default(app=None):
  43. if app is None:
  44. return _state.get_current_app()
  45. return app
  46. def _app_or_default_trace(app=None): # pragma: no cover
  47. from traceback import print_stack
  48. try:
  49. from billiard.process import current_process
  50. except ImportError:
  51. current_process = None
  52. if app is None:
  53. if getattr(_state._tls, 'current_app', None):
  54. print('-- RETURNING TO CURRENT APP --') # noqa+
  55. print_stack()
  56. return _state._tls.current_app
  57. if not current_process or current_process()._name == 'MainProcess':
  58. raise Exception('DEFAULT APP')
  59. print('-- RETURNING TO DEFAULT APP --') # noqa+
  60. print_stack()
  61. return _state.default_app
  62. return app
  63. def enable_trace():
  64. global app_or_default
  65. app_or_default = _app_or_default_trace
  66. def disable_trace():
  67. global app_or_default
  68. app_or_default = _app_or_default
  69. if os.environ.get('CELERY_TRACE_APP'): # pragma: no cover
  70. enable_trace()
  71. else:
  72. disable_trace()
  73. App = Celery # XXX Compat
  74. def shared_task(*args, **kwargs):
  75. """Create shared tasks (decorator).
  76. Will return a proxy that always takes the task from the current apps
  77. task registry.
  78. This can be used by library authors to create tasks that will work
  79. for any app environment.
  80. Example:
  81. >>> from celery import Celery, shared_task
  82. >>> @shared_task
  83. ... def add(x, y):
  84. ... return x + y
  85. >>> app1 = Celery(broker='amqp://')
  86. >>> add.app is app1
  87. True
  88. >>> app2 = Celery(broker='redis://')
  89. >>> add.app is app2
  90. """
  91. def create_shared_task(**options):
  92. def __inner(fun):
  93. name = options.get('name')
  94. # Set as shared task so that unfinalized apps,
  95. # and future apps will load the task.
  96. connect_on_app_finalize(
  97. lambda app: app._task_from_fun(fun, **options)
  98. )
  99. # Force all finalized apps to take this task as well.
  100. for app in _get_active_apps():
  101. if app.finalized:
  102. with app._finalize_mutex:
  103. app._task_from_fun(fun, **options)
  104. # Return a proxy that always gets the task from the current
  105. # apps task registry.
  106. def task_by_cons():
  107. app = current_app()
  108. return app.tasks[
  109. name or app.gen_task_name(fun.__name__, fun.__module__)
  110. ]
  111. return Proxy(task_by_cons)
  112. return __inner
  113. if len(args) == 1 and callable(args[0]):
  114. return create_shared_task(**kwargs)(args[0])
  115. return create_shared_task(*args, **kwargs)