我正在尝试为特定功能实现超时.我已经检查过SE中的许多问题,但找不到适合我问题的任何解决方案,因为
我正在为特定任务开发一个已经设计的自定义模块(比如MyModule),并且在其中定义了函数.其中一个函数(比如MyFunc)由于外部因素而倾向于永远运行,我只是不希望python脚本挂起.
我打算添加一个超时功能,如下面的伪代码所示
import MyModule
set_timeout(T)
MyResult=MyModule.MyFunc()
#Come to this part of script after execution of MyFunc() or after T seconds (the latter on priority)
if Timeout occurred:
print 'MyFunc did not execute completely'
else:
print 'MyFunc completed'
Run Code Online (Sandbox Code Playgroud)
但我不确定哪个模块可用于在python上实现这一点.请注意,我是一个新手,我编写的所有脚本都直接基于SE Answers或Python文档.