title: User input to break infinite loop?
body: <p>I'm working on calculating a bunch of triangles with special properties for a Number Theorist friend. There are infinitely many of these triangles, but they require a lot of computational power to find. </p>
  
  <p>We've got an infinite loop running through different b,d combinations. When the program ends, it calls the go(dict) function to export the triangles it found. Currently, we tell the program at the start what interval of time to run for. This is causing problems when we realize we need the computing power for something else, but the program still has hours to run and we don't want to lose the triangles it has already calculated by exiting the program without running go(dict).</p>
  
  <p>Ideally, we want some user input to cause the program to break the loop, run go(dict) with whatever current version of the dictionary it is holding in memory, then exit. Trying with atexit.register(go, dict) was unsuccessful, as it is called many times within the loop and runs many times when the program is terminated.</p>
  
  <p>(See the abbreviated loop code below)</p>
  
  <pre><code>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)&gt;interval:
          go(dict)
          return
      b +=1
  </code></pre>