【发布时间】:2012-07-09 08:05:10
【问题描述】:
我希望我的 Python 程序运行一个算法给定的秒数,然后打印迄今为止最好的结果并结束。
最好的方法是什么?
我尝试了以下方法,但没有成功(打印后程序继续运行):
def printBestResult(self):
print(self.bestResult)
sys.exit()
def findBestResult(self,time):
self.t = threading.Timer(time, self.printBestResult)
self.t.start()
while(1):
# find best result
【问题讨论】:
-
您能否在超时后将
SIGINT传递给它,并将SIGINT作为异常捕获并打印迄今为止的最佳结果并在该除块内退出?
标签: python multithreading time