PythonでRubyのタイムアウトに似た機能を実装するための良い解決策を知っている人はいますか? 私はそれをグーグルで検索しましたが、本当に良いものは何も見当たりませんでした。助けてくれてありがとう。
Rubyドキュメントへのリンクは次のとおりです http://www.ruby-doc.org/stdlib/libdoc/timeout/rdoc/index.html
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
から: