如何在python中使函数超时

时间:2019-01-11 13:10:58

标签: python

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

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

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

我尝试使用Timeout on a function call和timeout-decorator库中提到的解决方案。

但是,两个解决方案都返回了以下相同的错误。

signal.signal(signal.SIGALRM, handler)

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

有没有办法在python 3中执行超时?请让我知道是否需要更多详细信息。

1 个答案:

答案 0 :(得分:1)

该信号包是UNIX / Linux包的一部分,需要安装。 如果您在Windows上,请检查一下 https://github.com/Unity-Technologies/ml-agents/issues/7