def async_raise_in_main_thread(exc, use_concurrent_loop=True):
"""
Uses a unix signal to raise an exception to be raised in the main thread.
"""
from plumbum import local
pid = os.getpid()
if not REGISTERED_SIGNAL:
raise NotInitialized()
# sometimes the signal isn't caught by the main-thread, so we should try a few times (WEKAPP-14543)
def do_signal(raised_exc):
global LAST_ERROR
if LAST_ERROR is not raised_exc:
_logger.debug("MainThread took the exception - we're done here")
if use_concurrent_loop:
raiser.stop()
return
_logger.info("Raising %s in main thread", type(LAST_ERROR))
local.cmd.kill("-%d" % REGISTERED_SIGNAL, pid)
if use_concurrent_loop:
raiser = concurrent(do_signal, raised_exc=exc, loop=True, sleep=30, daemon=True, throw=False)
raiser.start()
else:
do_signal(exc)
评论列表
文章目录