_state.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # -*- coding: utf-8 -*-
  2. """Internal state.
  3. This is an internal module containing thread state
  4. like the ``current_app``, and ``current_task``.
  5. This module shouldn't be used directly.
  6. """
  7. from __future__ import absolute_import, print_function, unicode_literals
  8. import os
  9. import sys
  10. import threading
  11. import weakref
  12. from celery.local import Proxy
  13. from celery.utils.threads import LocalStack
  14. __all__ = [
  15. 'set_default_app', 'get_current_app', 'get_current_task',
  16. 'get_current_worker_task', 'current_app', 'current_task',
  17. 'connect_on_app_finalize',
  18. ]
  19. #: Global default app used when no current app.
  20. default_app = None
  21. #: List of all app instances (weakrefs), mustn't be used directly.
  22. _apps = weakref.WeakSet()
  23. #: Global set of functions to call whenever a new app is finalized.
  24. #: Shared tasks, and built-in tasks are created by adding callbacks here.
  25. _on_app_finalizers = set()
  26. _task_join_will_block = False
  27. def connect_on_app_finalize(callback):
  28. """Connect callback to be called when any app is finalized."""
  29. _on_app_finalizers.add(callback)
  30. return callback
  31. def _announce_app_finalized(app):
  32. callbacks = set(_on_app_finalizers)
  33. for callback in callbacks:
  34. callback(app)
  35. def _set_task_join_will_block(blocks):
  36. global _task_join_will_block
  37. _task_join_will_block = blocks
  38. def task_join_will_block():
  39. return _task_join_will_block
  40. class _TLS(threading.local):
  41. #: Apps with the :attr:`~celery.app.base.BaseApp.set_as_current` attribute
  42. #: sets this, so it will always contain the last instantiated app,
  43. #: and is the default app returned by :func:`app_or_default`.
  44. current_app = None
  45. _tls = _TLS()
  46. _task_stack = LocalStack()
  47. def set_default_app(app):
  48. """Set default app."""
  49. global default_app
  50. default_app = app
  51. def _get_current_app():
  52. if default_app is None:
  53. #: creates the global fallback app instance.
  54. from celery.app import Celery
  55. set_default_app(Celery(
  56. 'default', fixups=[], set_as_current=False,
  57. loader=os.environ.get('CELERY_LOADER') or 'default',
  58. ))
  59. return _tls.current_app or default_app
  60. def _set_current_app(app):
  61. _tls.current_app = app
  62. if os.environ.get('C_STRICT_APP'): # pragma: no cover
  63. def get_current_app():
  64. """Return the current app."""
  65. raise Exception('USES CURRENT APP')
  66. import traceback
  67. print('-- USES CURRENT_APP', file=sys.stderr) # noqa+
  68. traceback.print_stack(file=sys.stderr)
  69. return _get_current_app()
  70. else:
  71. get_current_app = _get_current_app
  72. def get_current_task():
  73. """Currently executing task."""
  74. return _task_stack.top
  75. def get_current_worker_task():
  76. """Currently executing task, that was applied by the worker.
  77. This is used to differentiate between the actual task
  78. executed by the worker and any task that was called within
  79. a task (using ``task.__call__`` or ``task.apply``)
  80. """
  81. for task in reversed(_task_stack.stack):
  82. if not task.request.called_directly:
  83. return task
  84. #: Proxy to current app.
  85. current_app = Proxy(get_current_app)
  86. #: Proxy to current task.
  87. current_task = Proxy(get_current_task)
  88. def _register_app(app):
  89. _apps.add(app)
  90. def _deregister_app(app):
  91. _apps.discard(app)
  92. def _get_active_apps():
  93. return _apps