python - 如何让Python中的函数超时

标签 python

我正在为我的语料库中的每个句子调用一个名为 func 的函数,如下所示。

count =0    
for sentence in sentences:
   count += 1
   results.append(func(sentence))

我想通过使用超时来传递处理时间太长的句子(例如,30秒后超时并打印句子编号)。

我尝试使用:Timeout on a function call中提到的解决方案以及超时装饰器库。

但是,这两种解决方案都返回了下面提到的相同错误。

signal.signal(signal.SIGALRM, handler)

AttributeError: module 'signal' has no attribute 'SIGALRM'

有没有办法在Python 3中执行超时?如果需要更多详细信息,请告诉我。

最佳答案

信号包是 UNIX/Linux 包的一部分,需要安装。 如果您使用的是 Windows,请查看此内容 https://github.com/Unity-Technologies/ml-agents/issues/7

关于python - 如何让Python中的函数超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54147226/

相关文章:

python - GAE - 运行超过 60 秒的预定 cron 作业

python - 使用 Google Apps 脚本 API 可执行文件的服务器到服务器身份验证

Python高级切片

python - 检查 csv 文件是否具有相同的项目

python - 在 Windows 中从源代码构建 statsmodels 时出错

python - Django - 属性错误 : module 'os' has no attribute 'environment'

python - 在函数中的选项周围添加引号

python - flask 属性错误 : module 'app' has no attribute 'run'

python - 将项目插入列表中的列表

python tkinter : unresolved functions of text widget