Skip to content

Instantly share code, notes, and snippets.

@colindix
Created October 19, 2020 07:11
Show Gist options
  • Save colindix/9fbd4566570396ae6541f3f453c2e81a to your computer and use it in GitHub Desktop.
Save colindix/9fbd4566570396ae6541f3f453c2e81a to your computer and use it in GitHub Desktop.
sigint handler
def graceful_cleanup(signum, frame):
"""
signal handler function should perform all clean up tasks and optionall exit.
I here it is just shutting down an executor which will exit the main loop,
i have also used it to set a flag so that processing in other threads will exit on the next test of that flag.
The code below is unlikely to work as i was just using it in a test harness, but the signal module does work well for this
use case.
"""
try:
futurelist_lock.release()
except RuntimeError as rte:
print("Lock on futurelist is not set")
print("Executing graceful shutdown")
e.shutdown(wait=False)
print("Have shutdown executor")
for f in futurelist.keys():
#if f.running():
print(f"Cancelling thread {futurelist[f]}")
print(f"{futurelist[f]} cancel operation returned: {f.cancel()}")
print(f"Cancelled status of {futurelist[f]} is {f.cancelled()}")
#
## main
# signal handler must be defined in main()
signal.signal(signal.SIGINT, graceful_cleanup)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment