__init__.py 5.8 KB

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