我想调用一个func,并在一分钟后阻止它运行,返回一个值.
def foo(txt):
best_value=-1
for word in txt:
if value(word)>best_value:
best_value=value(world)
return best_value
我想在一分钟后停止 run 并返回最佳值.
我想调用一个func,并在一分钟后阻止它运行,返回一个值.
def foo(txt):
best_value=-1
for word in txt:
if value(word)>best_value:
best_value=value(world)
return best_value
我想在一分钟后停止 run 并返回最佳值.
如果要将计时代码与业务逻辑分离,并将开销保持在最低限度,一种简单的方法是在代码中使用全局变量来告诉它何时停止运行:
time_out = False
def foo(txt):
best_value=-1
for word in txt:
if value(word)>best_value:
best_value=value(world)
if time_out:
break
return best_value
然后使用一个单独的函数绑定到一个Timer来停止它:
def send_stop():
global time_out
time_out = True
Timer(60.0, send_stop).start()
best_value = foo(corpus)