我知道"信号"在python 2.7中的模块,一般来说,我们可以使用以下代码来超时主线程函数调用:
import signal
def signal_handle_timeout:
raise Exception("Timeout!")
signal.signal(signal.SIGALRM, signal_handle_timeout)
signal.alarm(seconds)
try:
long_time_function()
except Exception as ex:
print ex
但它只能在主线程中运行,如果尝试在子线程中运行,则会抛出此错误:' ValueError:signal仅适用于主线程'
在Python中,我们如何在子线程中超时函数调用?