【发布时间】:2021-01-11 12:36:51
【问题描述】:
我正在使用线程 python 模块。我想执行一个运行用户输入的表达式的函数。我想等待它完成执行或直到达到超时期限。以下代码应在 5 秒后超时,但永远不会超时。
def longFunc():
# this expression could be entered by the user
return 45 ** 10 ** 1000
thread = threading.Thread(target=longFunc, args=(), daemon=True)
thread.start()
thread.join(5.0)
print("end") # never reaches this point :(
为什么会这样,我该如何解决这个问题?我应该尝试改用多处理吗?
【问题讨论】:
标签: python multithreading python-multiprocessing python-multithreading