Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

_state.py 3.8KB

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