_state.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. __all__ = ['set_default_app', 'get_current_app', 'get_current_task',
  17. 'get_current_worker_task', 'current_app', 'current_task']
  18. #: Global default app used when no current app.
  19. default_app = None
  20. #: List of all app instances (weakrefs), must not be used directly.
  21. _apps = weakref.WeakSet()
  22. _task_join_will_block = False
  23. def _set_task_join_will_block(blocks):
  24. global _task_join_will_block
  25. _task_join_will_block = blocks
  26. def task_join_will_block():
  27. return _task_join_will_block
  28. class _TLS(threading.local):
  29. #: Apps with the :attr:`~celery.app.base.BaseApp.set_as_current` attribute
  30. #: sets this, so it will always contain the last instantiated app,
  31. #: and is the default app returned by :func:`app_or_default`.
  32. current_app = None
  33. _tls = _TLS()
  34. _task_stack = LocalStack()
  35. def set_default_app(app):
  36. global default_app
  37. default_app = app
  38. def _get_current_app():
  39. if default_app is None:
  40. #: creates the global fallback app instance.
  41. from celery.app import Celery
  42. set_default_app(Celery(
  43. 'default',
  44. loader=os.environ.get('CELERY_LOADER') or 'default',
  45. fixups=[],
  46. set_as_current=False, accept_magic_kwargs=True,
  47. ))
  48. return _tls.current_app or default_app
  49. C_STRICT_APP = os.environ.get('C_STRICT_APP')
  50. if os.environ.get('C_STRICT_APP'): # pragma: no cover
  51. def get_current_app():
  52. raise Exception('USES CURRENT APP')
  53. import traceback
  54. print('-- USES CURRENT_APP', file=sys.stderr) # noqa+
  55. traceback.print_stack(file=sys.stderr)
  56. return _get_current_app()
  57. else:
  58. get_current_app = _get_current_app
  59. def get_current_task():
  60. """Currently executing task."""
  61. return _task_stack.top
  62. def get_current_worker_task():
  63. """Currently executing task, that was applied by the worker.
  64. This is used to differentiate between the actual task
  65. executed by the worker and any task that was called within
  66. a task (using ``task.__call__`` or ``task.apply``)
  67. """
  68. for task in reversed(_task_stack.stack):
  69. if not task.request.called_directly:
  70. return task
  71. #: Proxy to current app.
  72. current_app = Proxy(get_current_app)
  73. #: Proxy to current task.
  74. current_task = Proxy(get_current_task)
  75. def _register_app(app):
  76. _apps.add(app)
  77. def _get_active_apps():
  78. return _apps