|
@@ -38,12 +38,12 @@ if os.environ.get('C_IMPDEBUG'): # pragma: no cover
|
|
|
return real_import(name, locals, globals, fromlist, level)
|
|
|
builtins.__import__ = debug_import
|
|
|
|
|
|
+# This is never executed, but tricks static analyzers (PyDev, PyCharm,
|
|
|
+# pylint, etc.) into knowing the types of these symbols, and what
|
|
|
+# they contain.
|
|
|
STATICA_HACK = True
|
|
|
globals()['kcah_acitats'[::-1].upper()] = False
|
|
|
if STATICA_HACK: # pragma: no cover
|
|
|
- # This is never executed, but tricks static analyzers (PyDev, PyCharm,
|
|
|
- # pylint, etc.) into knowing the types of these symbols, and what
|
|
|
- # they contain.
|
|
|
from celery.app import shared_task # noqa
|
|
|
from celery.app.base import Celery # noqa
|
|
|
from celery.app.utils import bugreport # noqa
|
|
@@ -54,6 +54,72 @@ if STATICA_HACK: # pragma: no cover
|
|
|
)
|
|
|
from celery.utils import uuid # noqa
|
|
|
|
|
|
+# Eventlet/gevent patching must happen before importing
|
|
|
+# anything else, so these tools must be at top-level.
|
|
|
+
|
|
|
+
|
|
|
+def _find_option_with_arg(argv, short_opts=None, long_opts=None):
|
|
|
+ """Search argv for option specifying its short and longopt
|
|
|
+ alternatives.
|
|
|
+
|
|
|
+ Returns the value of the option if found.
|
|
|
+
|
|
|
+ """
|
|
|
+ for i, arg in enumerate(argv):
|
|
|
+ if arg.startswith('-'):
|
|
|
+ if long_opts and arg.startswith('--'):
|
|
|
+ name, _, val = arg.partition('=')
|
|
|
+ if name in long_opts:
|
|
|
+ return val
|
|
|
+ if short_opts and arg in short_opts:
|
|
|
+ return argv[i + 1]
|
|
|
+ raise KeyError('|'.join(short_opts or [] + long_opts or []))
|
|
|
+
|
|
|
+
|
|
|
+def _patch_eventlet():
|
|
|
+ import eventlet
|
|
|
+ import eventlet.debug
|
|
|
+ eventlet.monkey_patch()
|
|
|
+ EVENTLET_DBLOCK = int(os.environ.get('EVENTLET_NOBLOCK', 0))
|
|
|
+ if EVENTLET_DBLOCK:
|
|
|
+ eventlet.debug.hub_blocking_detection(EVENTLET_DBLOCK)
|
|
|
+
|
|
|
+
|
|
|
+def _patch_gevent():
|
|
|
+ from gevent import monkey, version_info
|
|
|
+ monkey.patch_all()
|
|
|
+ if version_info[0] == 0: # pragma: no cover
|
|
|
+ # Signals aren't working in gevent versions <1.0,
|
|
|
+ # and are not monkey patched by patch_all()
|
|
|
+ from gevent import signal as _gevent_signal
|
|
|
+ _signal = __import__('signal')
|
|
|
+ _signal.signal = _gevent_signal
|
|
|
+
|
|
|
+
|
|
|
+def maybe_patch_concurrency(argv=sys.argv,
|
|
|
+ short_opts=['-P'], long_opts=['--pool'],
|
|
|
+ patches={'eventlet': _patch_eventlet,
|
|
|
+ 'gevent': _patch_gevent}):
|
|
|
+ """With short and long opt alternatives that specify the command line
|
|
|
+ option to set the pool, this makes sure that anything that needs
|
|
|
+ to be patched is completed as early as possible.
|
|
|
+ (e.g. eventlet/gevent monkey patches)."""
|
|
|
+ try:
|
|
|
+ pool = _find_option_with_arg(argv, short_opts, long_opts)
|
|
|
+ except KeyError:
|
|
|
+ pass
|
|
|
+ else:
|
|
|
+ try:
|
|
|
+ patcher = patches[pool]
|
|
|
+ except KeyError:
|
|
|
+ pass
|
|
|
+ else:
|
|
|
+ print('PATCHING CONCURRENCY USING %r' % (patcher, ))
|
|
|
+ patcher()
|
|
|
+ # set up eventlet/gevent environments ASAP.
|
|
|
+ from celery import concurrency
|
|
|
+ concurrency.get_implementation(pool)
|
|
|
+
|
|
|
# Lazy loading
|
|
|
from .five import recreate_module
|
|
|
|
|
@@ -73,6 +139,7 @@ old_module, new_module = recreate_module( # pragma: no cover
|
|
|
__author__=__author__, __contact__=__contact__,
|
|
|
__homepage__=__homepage__, __docformat__=__docformat__,
|
|
|
VERSION=VERSION, SERIES=SERIES, VERSION_BANNER=VERSION_BANNER,
|
|
|
+ maybe_patch_concurrency=maybe_patch_concurrency,
|
|
|
)
|
|
|
|
|
|
|