'How to receive and dispatch signals (SIGINT, SIGTERM) in Pyhon, if Thread running in Process

So, i want to receive and handle SIGINT and SEGTERM signals for terminate the program. In MainThread I start Process, then in Process running Thread and in Thread run a loop. Below you can see the example of the code.

import signal
from multiprocessing import Process
from threading import Thread


def in_thread():
    try:
        while True:
            pass
    except Exception as e:
        print(e)


def in_process():
    signal.signal(signal.SIGINT, lambda _, __: print("SIGINT"))
    signal.signal(signal.SIGTERM, lambda _, __: print("SIGTERM"))
    try:
        t = Thread(target=in_thread)
        t.start()
        t.join()
    except Exception as e:
        print(e)


if __name__ == '__main__':
    signal.signal(signal.SIGINT, lambda _, __: print("SIGINT"))
    signal.signal(signal.SIGTERM, lambda _, __: print("SIGTERM"))
    try:
        p = Process(target=in_process)
        p.start()
        p.join()
    except Exception as e:
        print(e)

If you run this code from the shell and press CTRL+C, nothing happens, nothing is printed. So how can i terminate the program just using CTRL+C, without any timeouts and so on? Thanks!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source