__init__.py 5.9 KB

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