def _gevent_will_monkey_patch(native_module, items, warn): # pylint:disable=unused-argument # Make sure the MainThread can be found by our current greenlet ID, # otherwise we get a new DummyThread, which cannot be joined. # Fixes tests in test_threading_2 under PyPy. main_thread = main_native_thread() if __threading__.current_thread() != main_thread: warn("Monkey-patching outside the main native thread. Some APIs " "will not be available. Expect a KeyError to be printed at shutdown.") return if _get_ident() not in __threading__._active: main_id = main_thread.ident del __threading__._active[main_id] main_thread._ident = main_thread._Thread__ident = _get_ident() __threading__._active[_get_ident()] = main_thread
def __init__(self): # pylint:disable=super-init-not-called #_DummyThread_.__init__(self) # It'd be nice to use a pattern like "greenlet-%d", but maybe somebody out # there is checking thread names... self._name = self._Thread__name = __threading__._newname("DummyThread-%d") # All dummy threads in the same native thread share the same ident # (that of the native thread) self._set_ident() g = getcurrent() gid = _get_ident(g) __threading__._active[gid] = self rawlink = getattr(g, 'rawlink', None) if rawlink is not None: # raw greenlet.greenlet greenlets don't # have rawlink... rawlink(_cleanup) else: # ... so for them we use weakrefs. # See https://github.com/gevent/gevent/issues/918 global _weakref if _weakref is None: _weakref = __import__('weakref') ref = _weakref.ref(g, _make_cleanup_id(gid)) self.__raw_ref = ref
def __init__(self): # pylint:disable=super-init-not-called #_DummyThread_.__init__(self) # It'd be nice to use a pattern like "greenlet-%d", but there are definitely # third-party libraries checking thread names to detect DummyThread objects. self._name = self._Thread__name = __threading__._newname("Dummy-%d") # All dummy threads in the same native thread share the same ident # (that of the native thread), unless we're monkey-patched. self._set_ident() g = getcurrent() gid = _get_ident(g) __threading__._active[gid] = self rawlink = getattr(g, 'rawlink', None) if rawlink is not None: # raw greenlet.greenlet greenlets don't # have rawlink... rawlink(_cleanup) else: # ... so for them we use weakrefs. # See https://github.com/gevent/gevent/issues/918 ref = self.__weakref_ref ref = ref(g, _make_cleanup_id(gid)) # pylint:disable=too-many-function-args self.__raw_ref = ref assert self.__raw_ref is ref # prevent pylint thinking its unused
def __init__(self): #_DummyThread_.__init__(self) # pylint:disable=super-init-not-called # It'd be nice to use a pattern like "greenlet-%d", but maybe somebody out # there is checking thread names... self._name = self._Thread__name = __threading__._newname("DummyThread-%d") self._set_ident() __threading__._active[_get_ident()] = self g = getcurrent() rawlink = getattr(g, 'rawlink', None) if rawlink is not None: rawlink(_cleanup)
def __init__(self): _DummyThread_.__init__(self) g = getcurrent() rawlink = getattr(g, 'rawlink', None) if rawlink is not None: rawlink(_cleanup) def _Thread__stop(self): pass if PYPY: # Make sure the MainThread can be found by our current greenlet ID, # otherwise we get a new DummyThread, which cannot be joined. # Fixes tests in test_threading_2 if _get_ident() not in __threading__._active and len( __threading__._active) == 1: k, v = __threading__._active.items()[0] del __threading__._active[k] __threading__._active[_get_ident()] = v import sys if sys.version_info[:2] >= (3, 4): # XXX: Issue 18808 breaks us on Python 3.4. # Thread objects now expect a callback from the interpreter itself # (threadmodule.c:release_sentinel). Because this never happens # when a greenlet exits, join() and friends will block forever. # The solution below involves capturing the greenlet when it is # started and deferring the known broken methods to it. class Thread(__threading__.Thread):
def _Thread__stop(self): pass _stop = _Thread__stop # py3 def _wait_for_tstate_lock(self, *args, **kwargs): pass # Make sure the MainThread can be found by our current greenlet ID, # otherwise we get a new DummyThread, which cannot be joined. # Fixes tests in test_threading_2 under PyPy, and generally makes things nicer # when gevent.threading is imported before monkey patching or not at all # XXX: This assumes that the import is happening in the "main" greenlet if _get_ident() not in __threading__._active and len( __threading__._active) == 1: _k, _v = next(iter(__threading__._active.items())) del __threading__._active[_k] _v._Thread__ident = _get_ident() __threading__._active[_get_ident()] = _v del _k del _v # Avoid printing an error on shutdown trying to remove the thread entry # we just replaced if we're not fully monkey patched in # XXX: This causes a hang on PyPy for some unknown reason (as soon as class _active # defines __delitem__, shutdown hangs. Maybe due to something with the GC? # XXX: This may be fixed in 2.6.1+ if not PYPY: # pylint:disable=no-member
return __threading__.main_thread() # pylint:disable=no-member else: _main_threads = [(_k, _v) for _k, _v in __threading__._active.items() if isinstance(_v, __threading__._MainThread)] assert len(_main_threads) == 1, "Too many main threads" def main_native_thread(): return _main_threads[0][1] # Make sure the MainThread can be found by our current greenlet ID, # otherwise we get a new DummyThread, which cannot be joined. # Fixes tests in test_threading_2 under PyPy, and generally makes things nicer # when gevent.threading is imported before monkey patching or not at all # XXX: This assumes that the import is happening in the "main" greenlet/thread. # XXX: We should really only be doing this from gevent.monkey. if _get_ident() not in __threading__._active: _v = main_native_thread() _k = _v.ident del __threading__._active[_k] _v._ident = _v._Thread__ident = _get_ident() __threading__._active[_get_ident()] = _v del _k del _v # Avoid printing an error on shutdown trying to remove the thread entry # we just replaced if we're not fully monkey patched in # XXX: This causes a hang on PyPy for some unknown reason (as soon as class _active # defines __delitem__, shutdown hangs. Maybe due to something with the GC? # XXX: This may be fixed in 2.6.1+ if not PYPY: # pylint:disable=no-member
def __init__(self): _DummyThread_.__init__(self) g = getcurrent() rawlink = getattr(g, 'rawlink', None) if rawlink is not None: rawlink(_cleanup) def _Thread__stop(self): pass if PYPY: # Make sure the MainThread can be found by our current greenlet ID, # otherwise we get a new DummyThread, which cannot be joined. # Fixes tests in test_threading_2 if _get_ident() not in __threading__._active and len(__threading__._active) == 1: k, v = __threading__._active.items()[0] del __threading__._active[k] __threading__._active[_get_ident()] = v import sys if sys.version_info[:2] >= (3, 4): # XXX: Issue 18808 breaks us on Python 3.4. # Thread objects now expect a callback from the interpreter itself # (threadmodule.c:release_sentinel). Because this never happens # when a greenlet exits, join() and friends will block forever. # The solution below involves capturing the greenlet when it is # started and deferring the known broken methods to it. class Thread(__threading__.Thread): _greenlet = None
def _cleanup(g): __threading__._active.pop(_get_ident(g), None)
rawlink(_cleanup) def _Thread__stop(self): pass _stop = _Thread__stop # py3 def _wait_for_tstate_lock(self, *args, **kwargs): pass # Make sure the MainThread can be found by our current greenlet ID, # otherwise we get a new DummyThread, which cannot be joined. # Fixes tests in test_threading_2 under PyPy, and generally makes things nicer # when gevent.threading is imported before monkey patching or not at all # XXX: This assumes that the import is happening in the "main" greenlet if _get_ident() not in __threading__._active and len(__threading__._active) == 1: _k, _v = next(iter(__threading__._active.items())) del __threading__._active[_k] _v._Thread__ident = _get_ident() __threading__._active[_get_ident()] = _v del _k del _v # Avoid printing an error on shutdown trying to remove the thread entry # we just replaced if we're not fully monkey patched in # XXX: This causes a hang on PyPy for some unknown reason (as soon as class _active # defines __delitem__, shutdown hangs. Maybe due to something with the GC? # XXX: This may be fixed in 2.6.1+ if not PYPY: # pylint:disable=no-member _MAIN_THREAD = __threading__._get_ident() if hasattr(__threading__, '_get_ident') else __threading__.get_ident()