__init__.py 5.3 KB

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