title: Invoke a function when the program is terminated or pc shutdown
body: <p>my problem is that;</p>
  
  <p>I want to invoke a function when the program is terminated or computer shutdowned. </p>
  
  <p>I did search on the net , I found  atexit, here a sample program to tell what I want clearly</p>
  
  <pre><code>import atexit
  a = 1
  b = 0
  while a==1:
      b += 1
      #if b=30: 
          #a=2
  def say_bye():
      print " Goodbye "
  
  atexit.register(say_bye)
  </code></pre>
  
  <p>it works if recomment the comment section, but it is not that I want. it prints "Goodbye" when all code are executed not when terminated or pc shutdown.</p>
  
  <p>I hope it is clear, thanks in advance.</p>
  
  <p>Python 2.7
  Win 8 64</p>