Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Handle CTRL+C in Python
#!/usr/bin/env python
import signal
import sys
def signal_handler(signal, frame):
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
@erickinho1bra

This comment has been minimized.

Copy link

erickinho1bra commented Mar 7, 2019

Thank you for having the simplest solution to handing ctrl+c. People out there complicate things so much! haha

@dematom

This comment has been minimized.

Copy link

dematom commented Mar 11, 2020

cool

@leviethung2103

This comment has been minimized.

Copy link

leviethung2103 commented Jul 30, 2020

Thank you for your code. It works perfectly.

However, I would like to ask you about the function signal_handler. Do we need to define the parameters signal and frame into that function?

What are the meanings of signal and frame in the function?

Thank you in advance.

@rtfpessoa

This comment has been minimized.

Copy link
Owner Author

rtfpessoa commented Jul 30, 2020

signal_handler is just the fuction you are passing to signal.signal(..) in line 9.
This was done a long time ago, but those are just the parameters that the runtime give you so you can have more context about the signal.

@leviethung2103

This comment has been minimized.

Copy link

leviethung2103 commented Jul 30, 2020

Yes. I got your points. Thank you so much for this solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.