0

I installed the signal in the main method,

But when I pressed ctrl+c during running the process wasn't stopped,

exceptions.SystemExit: 0
^CKilled by user
Unhandled Error

EventTrigger and MemoryInfo are classes inherit from threading

and HttpStreamClient is a class inferits from twosted.reactor

How to kill my process by ctrl+c , thanks

Code

def signal_handler(*args):
    print("Killed by user")
    # teardown()
    sys.exit(0)

def install_signal():
    for sig in (SIGABRT, SIGILL, SIGINT, SIGSEGV, SIGTERM):
        signal(sig, signal_handler)



def main():
    try:
        global cgi, config
        install_signal()
        config = Config().read_file(sys.argv[1])[0]
        init_export_folder()
        setup_logging()

        threads = [
            EventTrigger(config),
            MemoryInfo(config),
        ]
        for thr in threads:
            thr.setDaemon(True)
            thr.start()

        HttpStreamClient(config).run()

        for thr in threads:
            thr.join()

    except BaseException as e:
        traceback.print_exc(file=sys.stdout)
        raise e
user3675188
  • 7,271
  • 11
  • 40
  • 76

2 Answers2

0

I think your problem might be the forceful nature that you are terminating the process.

While using twisted you should call reactor.stop() to get the initial run call to stop blocking.

If you change your signal_handler to shutdown the reactor.

def signal_handler(*args):
    print("Killed by user")
    reactor.stop()

Your threads could still keep the process alive. Thread.join doesn't forcefully stop a thread, which in general is never really a good idea. If EventTrigger or MemoryInfo are still running the thr.join will block. You will need a mechanism to stop threads. Maybe take a look here.

Community
  • 1
  • 1
Alex
  • 1,993
  • 1
  • 15
  • 25
0

sys.exit() raises a Python exception; I'm pretty sure raising an exception in a signal handler does not do much. Either call reactor.stop() as Alex says or use os._exit(0). Be aware that using os._exit(0) will terminate the process without further ado.

fraca7
  • 1,178
  • 5
  • 11