import threading
import signal


def run_edge_process(edge_proc):
    """ Runs a child hass process. Returns True if it should be restarted.  """
    requested_stop = threading.Event()
    edge_proc.daemon = True

    def request_stop(*args):
        """ request hass stop, *args is for signal handler callback """
        requested_stop.set()
        edge_proc.terminate()

    try:
        signal.signal(signal.SIGTERM, request_stop)
    except ValueError:
        print('Could not bind to SIGTERM. Are you running in a thread?')

    edge_proc.start()
    try:
        edge_proc.join()
    except KeyboardInterrupt:
        request_stop()
        try:
            edge_proc.join()
        except KeyboardInterrupt:
            return False

    return (not requested_stop.isSet() and
            edge_proc.exitcode == 100,
            edge_proc.exitcode)