擅长:python、mysql、java
<p>使用信号:</p>
<pre><code>import signal
interrupted = False # Used to break the loop when we send SIGINT
# When SIGINT is received, set interrupted to True
def signal_handler(signal, frame):
global interrupted
interrupted = True
# Sets signal_handler to run if a SIGINT was received
signal.signal(signal.SIGINT, signal_handler)
interval = eval(input("How many hours shall I run for? "))*3600
starttime = time.time()
dict = {}
b = start_value
while True:
for d in range (1, b):
compute stuff
if (condition):
add triangle to dict
if (time.time()-starttime)>interval:
go(dict)
break
if interrupted:
go(dict)
break
b +=1
</code></pre>
<p>现在,当我们点击<code>ctrl+c</code>时,我们将<code>interrupted</code>设置为<code>True</code>,它运行<code>go(dict)</code>,并打破循环。在</p>