【问题标题】:How to timeout a fuction in python如何使python中的函数超时
【发布时间】:2019-01-11 13:10:58
【问题描述】:

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

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 中执行超时?如果需要更多详细信息,请告诉我。

【问题讨论】:

标签: python


【解决方案1】:

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2011-01-12
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 2016-08-01
  • 1970-01-01
相关资源
最近更新 更多