擅长:python、mysql、java
<p>我经常使用这样的范例来完成一个“循环,直到我这么说”
差不多吧。你知道吗</p>
<pre><code>class UserKilledException(KeyboardInterrupt):
pass
try:
while True:
#do stuff
except UserKilledException:
#do cleanup here
</code></pre>
<p>只要让您的代码在用户决定以某种方式关闭应用程序时抛出一个<code>UserKilledException</code>。如果它是一个cli应用程序,那么<code>KeyboardInterrupt</code>就可以了。你知道吗</p>