def update_metadata_queue(self, agent: BaseAgent): """ Adds a new instance of AgentMetadata into the `agent_metadata_queue` using `agent` data. :param agent: An instance of an agent. """ pids = {os.getpid(), *agent.get_extra_pids()} helper_process_request = agent.get_helper_process_request() self.agent_metadata_queue.put( AgentMetadata(self.index, self.name, self.team, pids, helper_process_request))
def launch_bot_process_helper(self, early_starters_only=False, match_config: MatchConfig = None): # Start matchcomms here as it's only required for the bots. self.kill_matchcomms_server() self.matchcomms_server = launch_matchcomms_server() self.bot_processes = { ind: proc for ind, proc in self.bot_processes.items() if proc.is_alive() } num_started = 0 # Launch processes # TODO: this might be the right moment to fix the player indices based on a game tick packet. if not early_starters_only: packet = get_one_packet() # TODO: root through the packet and find discrepancies in the player index mapping. for i in range( min(self.num_participants, len(match_config.player_configs))): player_config = match_config.player_configs[i] if not player_config.has_bot_script(): continue if early_starters_only and not self.bot_bundles[ i].supports_early_start: continue spawn_id = player_config.spawn_id if early_starters_only: # Danger: we have low confidence in this since we're not leveraging the spawn id. participant_index = i else: participant_index = None self.logger.info( f'Player in slot {i} was sent with spawn id {spawn_id}, will search in the packet.' ) for n in range(0, packet.PlayersLength()): packet_spawn_id = packet.Players(n).SpawnId() if spawn_id == packet_spawn_id: self.logger.info( f'Looks good, considering participant index to be {n}' ) participant_index = n if participant_index is None: for prox_index, proc_info in self.bot_processes.items(): if spawn_id == proc_info.player_config.spawn_id: participant_index = prox_index if participant_index is None: raise Exception( f"Unable to determine the bot index for spawn id {spawn_id}" ) if participant_index not in self.bot_processes: bundle = get_bot_config_bundle(player_config.config_path) name = str(self.match_config.player_configs[i].deduped_name) if bundle.supports_standalone: executable = sys.executable if bundle.use_virtual_environment: executable = str( Path(bundle.config_directory) / 'venv' / 'Scripts' / 'python.exe') process = subprocess.Popen( [ executable, bundle.python_file, '--config-file', str(player_config.config_path), '--name', name, '--team', str(self.teams[i]), '--player-index', str(participant_index), '--spawn-id', str(spawn_id), '--matchcomms-url', self.matchcomms_server.root_url.geturl() ], cwd=Path(bundle.config_directory).parent) self.bot_processes[participant_index] = BotProcessInfo( process=None, subprocess=process, player_config=player_config) # Insert immediately into the agent metadata map because the standalone process has no way to communicate it back out self.agent_metadata_map[participant_index] = AgentMetadata( participant_index, name, self.teams[i], {process.pid}) else: reload_request = mp.Event() quit_callback = mp.Event() self.bot_reload_requests.append(reload_request) self.bot_quit_callbacks.append(quit_callback) process = mp.Process( target=SetupManager.run_agent, args=(self.quit_event, quit_callback, reload_request, self.bot_bundles[i], name, self.teams[i], participant_index, self.python_files[i], self.agent_metadata_queue, match_config, self.matchcomms_server.root_url, spawn_id)) process.start() self.bot_processes[participant_index] = BotProcessInfo( process=process, subprocess=None, player_config=player_config) num_started += 1 self.logger.info(f"Successfully started {num_started} bot processes") process_configuration.configure_processes(self.agent_metadata_map, self.logger) scripts_started = 0 for script_config in match_config.script_configs: script_config_bundle = get_script_config_bundle( script_config.config_path) if early_starters_only and not script_config_bundle.supports_early_start: continue executable = sys.executable if script_config_bundle.use_virtual_environment: executable = str( Path(script_config_bundle.config_directory) / 'venv' / 'Scripts' / 'python.exe') process = subprocess.Popen( [executable, script_config_bundle.script_file], cwd=Path(script_config_bundle.config_directory).parent) self.logger.info( f"Started script with pid {process.pid} using {process.args}") self.script_processes[process.pid] = process scripts_started += 1 self.logger.debug(f"Successfully started {scripts_started} scripts") return num_started