with GracefulInterruptHandler() as h:
for i in xrange(1000):
print "..."
time.sleep(1)
if h.interrupted:
print "interrupted!"
time.sleep(2)
break
嵌套处理程序:
with GracefulInterruptHandler() as h1:
while True:
print "(1)..."
time.sleep(1)
with GracefulInterruptHandler() as h2:
while True:
print "\t(2)..."
time.sleep(1)
if h2.interrupted:
print "\t(2) interrupted!"
time.sleep(2)
break
if h1.interrupted:
print "(1) interrupted!"
time.sleep(2)
break
作为上下文管理器:
使用:
嵌套处理程序:
从这里开始:https://gist.github.com/2907502
你可以把它当作一个例外(键盘中断),就像其他的一样。创建一个新文件并从shell运行它,其中包含以下内容,以了解我的意思:
相关问题 更多 >
编程相关推荐