__init__.py 4.3 KB

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