Skip to content

Instantly share code, notes, and snippets.

@eastside
Last active April 20, 2022 04:05
Show Gist options
  • Save eastside/8e7cad6e32d0809617e4153b54b47e59 to your computer and use it in GitHub Desktop.
Save eastside/8e7cad6e32d0809617e4153b54b47e59 to your computer and use it in GitHub Desktop.
Why does does commenting out line 44 fix this code?
import signal
from threading import Thread
import time
class MyThread(Thread):
def __init__(self):
super().__init__()
self._stopped = False
def run(self):
while not self._stopped:
time.sleep(0.2)
def stop(self):
self._stopped = True
class ThreadRunner:
def start(self):
self._my_thread = MyThread()
self._my_thread.start()
def end(self):
self._my_thread.stop()
self._my_thread.join()
print('ThreadRunner end!')
def loop_forever():
thread_runner = ThreadRunner()
try:
thread_runner.start()
yield
finally:
print('loop_forever() is all done!') # When does this line get run?
thread_runner.end()
def listener():
print('listener begin!')
looper = loop_forever()
next(looper)
try:
raise Exception()
except Exception as e:
es = e # If you comment out this line, it doesn't hang
print('listener... done!')
def main():
def handle_exit(signum, *args):
raise Exception("SIGINT: EXITING")
signal.signal(signal.SIGINT, handle_exit)
listener()
if __name__ == "__main__":
main()
print('This program has exited. Or has it?')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment