__init__.py 5.4 KB

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