线程由于超时而中止后,底层执行是否可以终止?
我遵循这里提供的以下线程方法:Python可调用超时
下面是针对Python3的此代码的修订版本。
此代码通过在调用实现的threading.Thread.abort()
时抛出RuntimeError
来正确地超时可调用函数,这与为threading.Thread.join(timeout)
分配超时值时所预期的一样。
但是,即使线程被中止,底层可调用程序仍在执行。
问题:在中止该线程以阻止底层可调用程序继续执行后,如何在不杀死父可执行程序的情况下杀死该线程?
这是我的代码:
import datetime as dt
import threading
def run_command_with_threaded_timeout(
func,
fargs=None,
fkwargs=None,
threaded_run_timeout_secs=None
):
class TimedThread(threading.Thread):
"""An abortable thread, by raising an exception inside its
context.
"""
def __init__(self):
super(TimedThread, self).__init__()
self.exc_info = (None, None, None)
def run(self):
self.started_at = dt.datetime.now()
try:
args = fargs if fargs else list()
kwargs = fkwargs if fkwargs else dict()
request_func = partial(func, *args, **kwargs)
self.result = request_func()
except:
# save the exception as an object attribute
self.exc_info = sys.exc_info()
self.result = None
self.ended_at = dt.datetime.now()
def abort(self):
self.ended_at = dt.datetime.now()
threaded_run_diff = self.ended_at - self.started_at
threaded_run_diff_secs = threaded_run_diff.seconds
if threaded_run_diff_secs >= threaded_run_timeout_secs:
raise RuntimeError(
"Threaded Run: Timed Out"
)
raise RuntimeError(
"Threaded Run: Aborted"
)
t = TimedThread()
t.start()
t.join(timeout=threaded_run_timeout_secs)
if t.exc_info[0] is not None: # if there were any exceptions
exc_type, exc_value, exc_traceback = t.exc_info
# Raise the exception/traceback inside the caller
raise exc_type.with_traceback(exc_value, exc_traceback)
if t.is_alive():
t.abort()
diff = t.ended_at - t.started_at
raise RuntimeError("%(f)s timed out after %(d)r seconds" %
{'f': func, 'd': diff.seconds})
return t.result
我读到的是线程不能从外部杀死。
可以做的是提供一个全局,由线程读取以确定它是否仍然有效。
global is_alive = True
kwargs = {
'config_job': config_job,
'config_request_params': config_request_params,
'config_request_credentials': config_request_credentials,
'config_request_retry': config_request_retry
}
try:
response = run_command_with_threaded_timeout(
func=cmd,
fargs=None,
fkwargs=kwargs,
timeout_sec=timeout_sec
)
except Exception as ex:
is_alive = False
raise