__init__.py 4.3 KB

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