2
有没有人知道一个很好的解决方案来实现一个类似于Ruby在Python中超时的功能?我使用了Google,并没有看到任何好的东西。谢谢您的帮助。Python的Ruby超时?
下面是来自于Ruby文档 http://www.ruby-doc.org/stdlib/libdoc/timeout/rdoc/index.html
有没有人知道一个很好的解决方案来实现一个类似于Ruby在Python中超时的功能?我使用了Google,并没有看到任何好的东西。谢谢您的帮助。Python的Ruby超时?
下面是来自于Ruby文档 http://www.ruby-doc.org/stdlib/libdoc/timeout/rdoc/index.html
def timeout(func, args=(), kwargs={}, timeout_duration=1, default=None):
import threading
class InterruptableThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.result = None
def run(self):
try:
self.result = func(*args, **kwargs)
except:
self.result = default
it = InterruptableThread()
it.start()
it.join(timeout_duration)
if it.isAlive():
return default
else:
return it.result
链接:
我想修改它作为装饰器使用。 – Dustin 2008-12-15 06:16:00
见http://stackoverflow.com/questions/366682/how-to-limit-execution函数调用python – tzot 2008-12-15 09:26:28