import os import signal import threading PID = os.getpid() run = True def print_hello(*unused_args): try: print("hello") except: signal.signal( signal.SIGUSR1, signal.SIG_IGN) raise def send_signals_constantly(): while run: #print("stuff") os.kill(PID, signal.SIGUSR1) def main(): global run signal.signal(signal.SIGUSR1, print_hello) t1 = threading.Thread(target=send_signals_constantly) t1.start() while run: try: print("world") except: run = False raise if __name__ == "__main__": main()