<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def daemons_stop(self, timeout=30, kill_children=False):
 """Stop the Alignak daemons Iterate over the self-launched daemons and their children list to send a TERM Wait for daemons to terminate and then send a KILL for those that are not yet stopped As a default behavior, only the launched daemons are killed, not their children. Each daemon will manage its children killing :param timeout: delay to wait before killing a daemon :type timeout: int :param kill_children: also kill the children (defaults to False) :type kill_children: bool :return: True if all daemons stopped """

        def on_terminate(proc):
            """Process termination callback function"""
            logger.debug("process %s terminated with exit code %s", proc.pid, proc.returncode)

        result = True

        if self.my_daemons:
            logger.info("Alignak self-launched daemons stop:")

            start = time.time()
            for daemon in list(self.my_daemons.values()):
                # Terminate the daemon and its children process
                procs = []
                if kill_children:
                    procs = daemon['process'].children()
                procs.append(daemon['process'])
                for process in procs:
                    try:
                        logger.info("- terminating process %s", process.name())
                        process.terminate()
                    except psutil.AccessDenied:
                        logger.warning("Process %s is %s", process.name(), process.status())

            procs = []
            for daemon in list(self.my_daemons.values()):
                # Stop the daemon and its children process
                if kill_children:
                    procs = daemon['process'].children()
                procs.append(daemon['process'])
            _, alive = psutil.wait_procs(procs, timeout=timeout, callback=on_terminate)
            if alive:
                # Kill processes
                for process in alive:
                    logger.warning("Process %s did not stopped, trying to kill", process.name())
                    process.kill()
                _, alive = psutil.wait_procs(alive, timeout=timeout, callback=on_terminate)
                if alive:
                    # give up
                    for process in alive:
                        logger.warning("process %s survived SIGKILL; giving up", process.name())
                        result = False

            logger.debug("Stopping daemons duration: %.2f seconds", time.time() - start)

        return result