python signal sigterm code example

Example 1: python catch sigterm

#!/usr/bin/python

from time import sleep
import signal
import sys


def sigterm_handler(_signo, _stack_frame):
    # Raises SystemExit(0):
    sys.exit(0)

if sys.argv[1] == "handle_signal":
    signal.signal(signal.SIGTERM, sigterm_handler)

try:
    print "Hello"
    i = 0
    while True:
        i += 1
        print "Iteration #%i" % i
        sleep(1)
finally:
    print "Goodbye"

Example 2: import signals

from django.db.models.signals import post_save
from django.contrib.auth.models import User
from django.dispatch import receiver

# Create your signals here.