def _launch_subprocesses(click_context: click.Context, agents: List[Path]) -> int: """ Launch many agents using subprocesses. :param click_context: the click context. :param agents: list of paths to agent projects. :return: execution status """ ctx = cast(Context, click_context.obj) launcher = AEALauncher( agents, mode="multiprocess", fail_policy=ExecutorExceptionPolicies.log_only, log_level=ctx.verbosity, ) try: launcher.start() except KeyboardInterrupt: logger.info("Keyboard interrupt detected.") finally: launcher.stop() for agent in launcher.failed: logger.info(f"Agent {agent} terminated with exit code 1") for agent in launcher.not_failed: logger.info(f"Agent {agent} terminated with exit code 0") return launcher.num_failed
def _launch_agents(click_context: click.core.Context, agents: List[str], multithreaded: bool) -> None: """ Run multiple agents. :param click_context: click context object. :param agents: agents names. :param multithreaded: bool flag to run as multithreads. :return: None. """ agents_directories = list(map(Path, list(OrderedDict.fromkeys(agents)))) mode = "threaded" if multithreaded else "multiprocess" ctx = cast(Context, click_context.obj) launcher = AEALauncher( agent_dirs=agents_directories, mode=mode, fail_policy=ExecutorExceptionPolicies.log_only, log_level=ctx.verbosity, ) try: """ run in threaded mode and wait for thread finished cause issue with python 3.6/3.7 on windows probably keyboard interrupt exception gets lost in executor pool or in asyncio module """ launcher.start(threaded=True) launcher.try_join_thread() except KeyboardInterrupt: logger.info("Keyboard interrupt detected.") finally: timeout: Optional[float] = None if os.name == "nt": # Windows bug: https://bugs.python.org/issue21822 timeout = 0 # pragma: nocover launcher.stop(timeout) for agent in launcher.failed: logger.info(f"Agent {agent} terminated with exit code 1") for agent in launcher.not_failed: logger.info(f"Agent {agent} terminated with exit code 0") logger.debug(f"Exit cli. code: {launcher.num_failed}") sys.exit(1 if launcher.num_failed > 0 else 0)
def test_one_fails(self) -> None: """Test agents started, one agent failed, exception is raised.""" try: runner = AEALauncher( [self.agent_name_1, self.agent_name_2, self.failing_agent], self.RUNNER_MODE, ) with pytest.raises(Exception, match="Expected exception!"): runner.start() finally: runner.stop()
def test_start_stop(self) -> None: """Test agents started stopped.""" try: runner = AEALauncher([self.agent_name_1, self.agent_name_2], self.RUNNER_MODE) runner.start(True) wait_for_condition(lambda: runner.is_running, timeout=10) assert runner.num_failed == 0 finally: runner.stop() assert not runner.is_running assert runner.num_failed == 0