def prompt(cleanup_function, signalnum=signal.SIGINT):
"""Give the user a decision prompt when ctrl+C is pressed."""
def cleanup_prompt(signum, frame):
# This handler is not re-entrant.
# Allow the user to exit immediately by sending the same signal
# a second time while in this block.
with handle(signalnum, sysexit):
choice = input(dedent("""
You pressed ctrl+C. What would you like to do?
- to exit immediately, press ctrl+C again
- to clean up and exit gracefully, enter 'c' or 'cleanup'
- to resume operation, press enter
(cleanup?): """))
if choice and 'cleanup'.casefold().startswith(choice.casefold()):
print('Cleaning up...')
cleanup_function()
print('Done cleaning up; exiting.')
sys.exit(1)
print('Continuing...')
return cleanup_prompt
评论列表
文章目录