Skip to main content
Code Review

Return to Question

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

I have code that needs to exit gracefully. To simplify things, I am presenting an example based on the answer given here here.

Since I need to handle a few signals, I thought of putting this logic in a function:

def set_signals():
 original_sigint = signal.getsignal(signal.SIGINT)
 signal.signal(signal.SIGINT, exit_gracefully)
 signal.signal(signal.SIGTERM, exit_gracefully)
 signal.signal(signal.SIGINT, exit_gracefully)
 signal.signal(signal.SIGALRM, exit_gracefully)
 signal.signal(signal.SIGHUP, signal.SIG_IGN)

Thus, the main block of the python code should be:

if __name__ == '__main__':
 # store the original SIGINT handler
 set_signals()
 run_program()

This will fail however, since the exit_gracefully does not know the variable original_init.

Therefore, my solution was to create original_sigint as a global variable.

import signal
import time
import sys
original_sigint = None
def run_program():
 while True:
 time.sleep(1)
 print("a")
def exit_gracefully(signum, frame):
 # restore the original signal handler as otherwise evil things will happen
 # in raw_input when CTRL+C is pressed, and our signal handler is not re-entrant
 global original_sigint
 signal.signal(signal.SIGINT, original_sigint)
 try:
 if raw_input("\nReally quit? (y/n)> ").lower().startswith('y'):
 sys.exit(1)
 except KeyboardInterrupt:
 print("Ok ok, quitting")
 sys.exit(1)
 # restore the exit gracefully handler here
 signal.signal(signal.SIGINT, exit_gracefully)
def set_signals():
 global original_sigint
 original_sigint = signal.getsignal(signal.SIGINT)
 signal.signal(signal.SIGINT, exit_gracefully)
 signal.signal(signal.SIGTERM, exit_gracefully)
 signal.signal(signal.SIGINT, exit_gracefully)
 signal.signal(signal.SIGALRM, exit_gracefully)
 signal.signal(signal.SIGHUP, signal.SIG_IGN)
if __name__ == '__main__':
 # store the original SIGINT handler
 set_signals()
 run_program()

This approach works, but I am not happy with the use of another global variable (the code I working on is quite and is already littered with those). So the question is, how would you do it differently?

I have code that needs to exit gracefully. To simplify things, I am presenting an example based on the answer given here.

Since I need to handle a few signals, I thought of putting this logic in a function:

def set_signals():
 original_sigint = signal.getsignal(signal.SIGINT)
 signal.signal(signal.SIGINT, exit_gracefully)
 signal.signal(signal.SIGTERM, exit_gracefully)
 signal.signal(signal.SIGINT, exit_gracefully)
 signal.signal(signal.SIGALRM, exit_gracefully)
 signal.signal(signal.SIGHUP, signal.SIG_IGN)

Thus, the main block of the python code should be:

if __name__ == '__main__':
 # store the original SIGINT handler
 set_signals()
 run_program()

This will fail however, since the exit_gracefully does not know the variable original_init.

Therefore, my solution was to create original_sigint as a global variable.

import signal
import time
import sys
original_sigint = None
def run_program():
 while True:
 time.sleep(1)
 print("a")
def exit_gracefully(signum, frame):
 # restore the original signal handler as otherwise evil things will happen
 # in raw_input when CTRL+C is pressed, and our signal handler is not re-entrant
 global original_sigint
 signal.signal(signal.SIGINT, original_sigint)
 try:
 if raw_input("\nReally quit? (y/n)> ").lower().startswith('y'):
 sys.exit(1)
 except KeyboardInterrupt:
 print("Ok ok, quitting")
 sys.exit(1)
 # restore the exit gracefully handler here
 signal.signal(signal.SIGINT, exit_gracefully)
def set_signals():
 global original_sigint
 original_sigint = signal.getsignal(signal.SIGINT)
 signal.signal(signal.SIGINT, exit_gracefully)
 signal.signal(signal.SIGTERM, exit_gracefully)
 signal.signal(signal.SIGINT, exit_gracefully)
 signal.signal(signal.SIGALRM, exit_gracefully)
 signal.signal(signal.SIGHUP, signal.SIG_IGN)
if __name__ == '__main__':
 # store the original SIGINT handler
 set_signals()
 run_program()

This approach works, but I am not happy with the use of another global variable (the code I working on is quite and is already littered with those). So the question is, how would you do it differently?

I have code that needs to exit gracefully. To simplify things, I am presenting an example based on the answer given here.

Since I need to handle a few signals, I thought of putting this logic in a function:

def set_signals():
 original_sigint = signal.getsignal(signal.SIGINT)
 signal.signal(signal.SIGINT, exit_gracefully)
 signal.signal(signal.SIGTERM, exit_gracefully)
 signal.signal(signal.SIGINT, exit_gracefully)
 signal.signal(signal.SIGALRM, exit_gracefully)
 signal.signal(signal.SIGHUP, signal.SIG_IGN)

Thus, the main block of the python code should be:

if __name__ == '__main__':
 # store the original SIGINT handler
 set_signals()
 run_program()

This will fail however, since the exit_gracefully does not know the variable original_init.

Therefore, my solution was to create original_sigint as a global variable.

import signal
import time
import sys
original_sigint = None
def run_program():
 while True:
 time.sleep(1)
 print("a")
def exit_gracefully(signum, frame):
 # restore the original signal handler as otherwise evil things will happen
 # in raw_input when CTRL+C is pressed, and our signal handler is not re-entrant
 global original_sigint
 signal.signal(signal.SIGINT, original_sigint)
 try:
 if raw_input("\nReally quit? (y/n)> ").lower().startswith('y'):
 sys.exit(1)
 except KeyboardInterrupt:
 print("Ok ok, quitting")
 sys.exit(1)
 # restore the exit gracefully handler here
 signal.signal(signal.SIGINT, exit_gracefully)
def set_signals():
 global original_sigint
 original_sigint = signal.getsignal(signal.SIGINT)
 signal.signal(signal.SIGINT, exit_gracefully)
 signal.signal(signal.SIGTERM, exit_gracefully)
 signal.signal(signal.SIGINT, exit_gracefully)
 signal.signal(signal.SIGALRM, exit_gracefully)
 signal.signal(signal.SIGHUP, signal.SIG_IGN)
if __name__ == '__main__':
 # store the original SIGINT handler
 set_signals()
 run_program()

This approach works, but I am not happy with the use of another global variable (the code I working on is quite and is already littered with those). So the question is, how would you do it differently?

edited tags
Link
Mathieu Guindon
  • 75.5k
  • 18
  • 194
  • 467
deleted 84 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

I have a code that needs to exit gracefully. To simplify things, I am presenting here an example based on the answer given here: python-catch-ctrl-c-command-prompt-really-want-to-quit-y-n-resume-executihere .

Since, I need to handle a few signals, I thought of putting this logic in a function:

This approach, works, but I am not happy with the use of another global variable (the code I working on, is quite and is already littered with those). So the question is, how would you do it differently?

I have a code that needs to exit gracefully. To simplify things, I am presenting here an example based on the answer given here: python-catch-ctrl-c-command-prompt-really-want-to-quit-y-n-resume-executi

Since, I need to handle a few signals I thought of putting this logic in a function:

This approach, works, but I am not happy with the use of another global variable (the code I working on, is quite and is already littered with those). So the question is, how would you do it differently?

I have code that needs to exit gracefully. To simplify things, I am presenting an example based on the answer given here .

Since I need to handle a few signals, I thought of putting this logic in a function:

This approach works, but I am not happy with the use of another global variable (the code I working on is quite and is already littered with those). So the question is, how would you do it differently?

Loading
Source Link
oz123
  • 507
  • 1
  • 5
  • 16
Loading
lang-py

AltStyle によって変換されたページ (->オリジナル) /