def execute(meth, *args, **kwargs):
"""
Execute *meth* in a Python thread, blocking the current coroutine/
greenthread until the method completes.
The primary use case for this is to wrap an object or module that is not
amenable to monkeypatching or any of the other tricks that Eventlet uses
to achieve cooperative yielding. With tpool, you can force such objects to
cooperate with green threads by sticking them in native threads, at the cost
of some overhead.
"""
setup()
# if already in tpool, don't recurse into the tpool
# also, call functions directly if we're inside an import lock, because
# if meth does any importing (sadly common), it will hang
my_thread = threading.currentThread()
if my_thread in _threads or imp.lock_held() or _nthreads == 0:
return meth(*args, **kwargs)
e = event.Event()
_reqq.put((e, meth, args, kwargs))
rv = e.wait()
if isinstance(rv, tuple) \
and len(rv) == 3 \
and isinstance(rv[1], EXC_CLASSES):
(c, e, tb) = rv
if not QUIET:
traceback.print_exception(c, e, tb)
traceback.print_stack()
six.reraise(c, e, tb)
return rv
python类lock_held()的实例源码
def __start_core(prefer, opts):
# Load the JVM dynamic library
from ._util import jvm_load
jvm_load(prefer)
# Start the JVM in its own thread
import threading, imp
from ._internal import jvm_create
jvm = jvm_create() # if the JVM is already created, this will raise an exception
event = threading.Event()
locked = imp.lock_held()
if locked:
# If we are in the middle of an import (which is definitely possible) we can't start
# another thread unless we release the import lock. If we do start the thread and then wait
# for it to start, it will deadlock Python
imp.release_lock()
try:
t = threading.Thread(target=jvm.run, name='JVM-Main', args=(opts,event))
t.daemon = True
t.start()
event.wait() # wait for it to be initialized
finally:
if locked: imp.acquire_lock()
jvm.check_pending_exception()
global __jvm
__jvm = jvm