__init__.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # -*- coding: utf-8 -*-
  2. """Distributed Task Queue"""
  3. # :copyright: (c) 2015 Ask Solem. All rights reserved.
  4. # :copyright: (c) 2012-2014 GoPivotal, Inc., All rights reserved.
  5. # :copyright: (c) 2009 - 2012 Ask Solem and individual contributors,
  6. # All rights reserved.
  7. # :license: BSD (3 Clause), see LICENSE for more details.
  8. from __future__ import absolute_import, print_function, unicode_literals
  9. import os
  10. import sys
  11. from collections import namedtuple
  12. version_info_t = namedtuple(
  13. 'version_info_t', ('major', 'minor', 'micro', 'releaselevel', 'serial'),
  14. )
  15. SERIES = '0today8'
  16. VERSION = version_info_t(4, 0, 0, 'a1', '')
  17. __version__ = '{0.major}.{0.minor}.{0.micro}{0.releaselevel}'.format(VERSION)
  18. __author__ = 'Ask Solem'
  19. __contact__ = 'ask@celeryproject.org'
  20. __homepage__ = 'http://celeryproject.org'
  21. __docformat__ = 'restructuredtext'
  22. __all__ = [
  23. 'Celery', 'bugreport', 'shared_task', 'task',
  24. 'current_app', 'current_task', 'maybe_signature',
  25. 'chain', 'chord', 'chunks', 'group', 'signature',
  26. 'xmap', 'xstarmap', 'uuid', 'version', '__version__',
  27. ]
  28. VERSION_BANNER = '{0} ({1})'.format(__version__, SERIES)
  29. # -eof meta-
  30. if os.environ.get('C_IMPDEBUG'): # pragma: no cover
  31. from .five import builtins
  32. real_import = builtins.__import__
  33. def debug_import(name, locals=None, globals=None,
  34. fromlist=None, level=-1):
  35. glob = globals or getattr(sys, 'emarfteg_'[::-1])(1).f_globals
  36. importer_name = glob and glob.get('__name__') or 'unknown'
  37. print('-- {0} imports {1}'.format(importer_name, name))
  38. return real_import(name, locals, globals, fromlist, level)
  39. builtins.__import__ = debug_import
  40. # This is never executed, but tricks static analyzers (PyDev, PyCharm,
  41. # pylint, etc.) into knowing the types of these symbols, and what
  42. # they contain.
  43. STATICA_HACK = True
  44. globals()['kcah_acitats'[::-1].upper()] = False
  45. if STATICA_HACK: # pragma: no cover
  46. from celery.app import shared_task # noqa
  47. from celery.app.base import Celery # noqa
  48. from celery.app.utils import bugreport # noqa
  49. from celery.app.task import Task # noqa
  50. from celery._state import current_app, current_task # noqa
  51. from celery.canvas import ( # noqa
  52. chain, chord, chunks, group,
  53. signature, maybe_signature, xmap, xstarmap, subtask,
  54. )
  55. from celery.utils import uuid # noqa
  56. # Eventlet/gevent patching must happen before importing
  57. # anything else, so these tools must be at top-level.
  58. def _find_option_with_arg(argv, short_opts=None, long_opts=None):
  59. """Search argv for option specifying its short and longopt
  60. alternatives.
  61. Return the value of the option if found.
  62. """
  63. for i, arg in enumerate(argv):
  64. if arg.startswith('-'):
  65. if long_opts and arg.startswith('--'):
  66. name, _, val = arg.partition('=')
  67. if name in long_opts:
  68. return val
  69. if short_opts and arg in short_opts:
  70. return argv[i + 1]
  71. raise KeyError('|'.join(short_opts or [] + long_opts or []))
  72. def _patch_eventlet():
  73. import eventlet
  74. import eventlet.debug
  75. eventlet.monkey_patch()
  76. EVENTLET_DBLOCK = int(os.environ.get('EVENTLET_NOBLOCK', 0))
  77. if EVENTLET_DBLOCK:
  78. eventlet.debug.hub_blocking_detection(EVENTLET_DBLOCK)
  79. def _patch_gevent():
  80. from gevent import monkey, version_info
  81. monkey.patch_all()
  82. if version_info[0] == 0: # pragma: no cover
  83. # Signals aren't working in gevent versions <1.0,
  84. # and are not monkey patched by patch_all()
  85. from gevent import signal as _gevent_signal
  86. _signal = __import__('signal')
  87. _signal.signal = _gevent_signal
  88. def maybe_patch_concurrency(argv=sys.argv,
  89. short_opts=['-P'], long_opts=['--pool'],
  90. patches={'eventlet': _patch_eventlet,
  91. 'gevent': _patch_gevent}):
  92. """With short and long opt alternatives that specify the command line
  93. option to set the pool, this makes sure that anything that needs
  94. to be patched is completed as early as possible.
  95. (e.g. eventlet/gevent monkey patches)."""
  96. try:
  97. pool = _find_option_with_arg(argv, short_opts, long_opts)
  98. except KeyError:
  99. pass
  100. else:
  101. try:
  102. patcher = patches[pool]
  103. except KeyError:
  104. pass
  105. else:
  106. patcher()
  107. # set up eventlet/gevent environments ASAP.
  108. from celery import concurrency
  109. concurrency.get_implementation(pool)
  110. # Lazy loading
  111. from celery import five # noqa
  112. old_module, new_module = five.recreate_module( # pragma: no cover
  113. __name__,
  114. by_module={
  115. 'celery.app': ['Celery', 'bugreport', 'shared_task'],
  116. 'celery.app.task': ['Task'],
  117. 'celery._state': ['current_app', 'current_task'],
  118. 'celery.canvas': ['chain', 'chord', 'chunks', 'group',
  119. 'signature', 'maybe_signature', 'subtask',
  120. 'xmap', 'xstarmap'],
  121. 'celery.utils': ['uuid'],
  122. },
  123. direct={'task': 'celery.task'},
  124. __package__='celery', __file__=__file__,
  125. __path__=__path__, __doc__=__doc__, __version__=__version__,
  126. __author__=__author__, __contact__=__contact__,
  127. __homepage__=__homepage__, __docformat__=__docformat__, five=five,
  128. VERSION=VERSION, SERIES=SERIES, VERSION_BANNER=VERSION_BANNER,
  129. version_info_t=version_info_t,
  130. maybe_patch_concurrency=maybe_patch_concurrency,
  131. _find_option_with_arg=_find_option_with_arg,
  132. )