Note that there are some explanatory texts on larger screens.

plurals
  1. POpython 2.6.x theading / signals /atexit fail on some versions?
    text
    copied!<p>I've seen a lot of questions related to this... but my code <em>works</em> on python 2.6.2 and <em>fails</em> to work on python 2.6.5. Am I wrong in thinking that the whole atexit "functions registered via this module are not called when the program is killed by a signal" thing shouldn't count here because I'm catching the signal and then exiting cleanly? What's going on here? Whats the proper way to do this?</p> <pre><code>import atexit, sys, signal, time, threading terminate = False threads = [] def test_loop(): while True: if terminate: print('stopping thread') break else: print('looping') time.sleep(1) @atexit.register def shutdown(): global terminate print('shutdown detected') terminate = True for thread in threads: thread.join() def close_handler(signum, frame): print('caught signal') sys.exit(0) def run(): global threads thread = threading.Thread(target=test_loop) thread.start() threads.append(thread) while True: time.sleep(2) print('main') signal.signal(signal.SIGINT, close_handler) if __name__ == "__main__": run() </code></pre> <p>python 2.6.2:</p> <pre><code>$ python halp.py looping looping looping main looping main looping looping looping main looping ^Ccaught signal shutdown detected stopping thread </code></pre> <p>python 2.6.5:</p> <pre><code>$ python halp.py looping looping looping main looping looping main looping looping main ^Ccaught signal looping looping looping looping ... looping looping Killed &lt;- kill -9 process at this point </code></pre> <p>The main thread on 2.6.5 appears to never execute the atexit functions.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload