Skip to content

Commit 30600ae

Browse files
authored
Merge pull request #88 from adf-python/fix/connect
adf-javaの接続形式と同じにする
2 parents 13c9aab + dabb446 commit 30600ae

22 files changed

+125
-42
lines changed

adf_core_python/core/agent/agent.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import sys
2+
import time as _time
23
from abc import abstractmethod
4+
from threading import Event
35
from typing import Any, Callable, NoReturn
46

57
from bitarray import bitarray
@@ -91,6 +93,7 @@ def __init__(
9193
data_storage_name: str,
9294
module_config: ModuleConfig,
9395
develop_data: DevelopData,
96+
finish_post_connect_event: Event,
9497
) -> None:
9598
self.name = name
9699
self.connect_request_id = None
@@ -102,6 +105,7 @@ def __init__(
102105
self.logger = get_logger(
103106
f"{self.__class__.__module__}.{self.__class__.__qualname__}"
104107
)
108+
self.finish_post_connect_event = finish_post_connect_event
105109

106110
self.team_name = team_name
107111
self.is_debug = is_debug
@@ -148,7 +152,7 @@ def post_connect(self) -> None:
148152
self._agent_info,
149153
)
150154

151-
self.logger.info(f"config: {self.config}")
155+
self.logger.debug(f"agent_config: {self.config}")
152156

153157
def update_step_info(
154158
self, time: int, change_set: ChangeSet, hear: list[Command]
@@ -293,9 +297,12 @@ def handler_sense(self, msg: Any) -> None:
293297
].intValue,
294298
)
295299
)
296-
297300
self.world_model.merge(change_set)
301+
start_update_info_time = _time.time()
298302
self.update_step_info(time, change_set, heard_commands)
303+
self.logger.debug(
304+
f"{time} step calculation time: {_time.time() - start_update_info_time}"
305+
)
299306

300307
def send_acknowledge(self, request_id: int) -> None:
301308
ak_ack = AKAcknowledge()

adf_core_python/core/agent/office/office.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from threading import Event
2+
13
from adf_core_python.core.agent.agent import Agent
24
from adf_core_python.core.agent.config.module_config import ModuleConfig
35
from adf_core_python.core.agent.develop.develop_data import DevelopData
@@ -18,6 +20,7 @@ def __init__(
1820
data_storage_name: str,
1921
module_config: ModuleConfig,
2022
develop_data: DevelopData,
23+
finish_post_connect_event: Event,
2124
) -> None:
2225
super().__init__(
2326
is_precompute,
@@ -27,6 +30,7 @@ def __init__(
2730
data_storage_name,
2831
module_config,
2932
develop_data,
33+
finish_post_connect_event,
3034
)
3135
self._tactics_center = tactics_center
3236
self._team_name = team_name
@@ -90,6 +94,7 @@ def post_connect(self) -> None:
9094
self.precompute_data,
9195
self._develop_data,
9296
)
97+
self.finish_post_connect_event.set()
9398

9499
def think(self) -> None:
95100
self._tactics_center.think(

adf_core_python/core/agent/office/office_ambulance.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from threading import Event
2+
13
from rcrs_core.connection.URN import Entity as EntityURN
24

35
from adf_core_python.core.agent.config.module_config import ModuleConfig
@@ -16,6 +18,7 @@ def __init__(
1618
data_storage_name: str,
1719
module_config: ModuleConfig,
1820
develop_data: DevelopData,
21+
finish_post_connect_event: Event,
1922
) -> None:
2023
super().__init__(
2124
tactics_center,
@@ -25,6 +28,7 @@ def __init__(
2528
data_storage_name,
2629
module_config,
2730
develop_data,
31+
finish_post_connect_event,
2832
)
2933

3034
def precompute(self) -> None:

adf_core_python/core/agent/office/office_fire.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from threading import Event
2+
13
from rcrs_core.connection.URN import Entity as EntityURN
24

35
from adf_core_python.core.agent.config.module_config import ModuleConfig
@@ -16,6 +18,7 @@ def __init__(
1618
data_storage_name: str,
1719
module_config: ModuleConfig,
1820
develop_data: DevelopData,
21+
finish_post_connect_event: Event,
1922
) -> None:
2023
super().__init__(
2124
tactics_center,
@@ -25,6 +28,7 @@ def __init__(
2528
data_storage_name,
2629
module_config,
2730
develop_data,
31+
finish_post_connect_event,
2832
)
2933

3034
def precompute(self) -> None:

adf_core_python/core/agent/office/office_police.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from threading import Event
2+
13
from rcrs_core.connection.URN import Entity as EntityURN
24

35
from adf_core_python.core.agent.config.module_config import ModuleConfig
@@ -16,6 +18,7 @@ def __init__(
1618
data_storage_name: str,
1719
module_config: ModuleConfig,
1820
develop_data: DevelopData,
21+
finish_post_connect_event: Event,
1922
) -> None:
2023
super().__init__(
2124
tactics_center,
@@ -25,6 +28,7 @@ def __init__(
2528
data_storage_name,
2629
module_config,
2730
develop_data,
31+
finish_post_connect_event,
2832
)
2933

3034
def precompute(self) -> None:

adf_core_python/core/agent/platoon/platoon.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from threading import Event
2+
13
from adf_core_python.core.agent.action.action import Action
24
from adf_core_python.core.agent.agent import Agent
35
from adf_core_python.core.agent.config.module_config import ModuleConfig
@@ -19,6 +21,7 @@ def __init__(
1921
data_storage_name: str,
2022
module_config: ModuleConfig,
2123
develop_data: DevelopData,
24+
finish_post_connect_event: Event,
2225
) -> None:
2326
super().__init__(
2427
is_precompute,
@@ -28,6 +31,7 @@ def __init__(
2831
data_storage_name,
2932
module_config,
3033
develop_data,
34+
finish_post_connect_event,
3135
)
3236
self._tactics_agent = tactics_agent
3337
self._team_name = team_name
@@ -92,6 +96,8 @@ def post_connect(self) -> None:
9296
self._develop_data,
9397
)
9498

99+
self.finish_post_connect_event.set()
100+
95101
def think(self) -> None:
96102
action: Action = self._tactics_agent.think(
97103
self._agent_info,

adf_core_python/core/agent/platoon/platoon_ambulance.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from threading import Event
2+
13
from rcrs_core.connection.URN import Entity as EntityURN
24

35
from adf_core_python.core.agent.config.module_config import ModuleConfig
@@ -16,6 +18,7 @@ def __init__(
1618
data_storage_name: str,
1719
module_config: ModuleConfig,
1820
develop_data: DevelopData,
21+
finish_post_connect_event: Event,
1922
):
2023
super().__init__(
2124
tactics_agent,
@@ -25,6 +28,7 @@ def __init__(
2528
data_storage_name,
2629
module_config,
2730
develop_data,
31+
finish_post_connect_event,
2832
)
2933

3034
def precompute(self) -> None:

adf_core_python/core/agent/platoon/platoon_fire.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from threading import Event
2+
13
from rcrs_core.connection.URN import Entity as EntityURN
24

35
from adf_core_python.core.agent.config.module_config import ModuleConfig
@@ -16,6 +18,7 @@ def __init__(
1618
data_storage_name: str,
1719
module_config: ModuleConfig,
1820
develop_data: DevelopData,
21+
finish_post_connect_event: Event,
1922
):
2023
super().__init__(
2124
tactics_agent,
@@ -25,6 +28,7 @@ def __init__(
2528
data_storage_name,
2629
module_config,
2730
develop_data,
31+
finish_post_connect_event,
2832
)
2933

3034
def precompute(self) -> None:

adf_core_python/core/agent/platoon/platoon_police.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from threading import Event
2+
13
from rcrs_core.connection.URN import Entity as EntityURN
24

35
from adf_core_python.core.agent.config.module_config import ModuleConfig
@@ -16,6 +18,7 @@ def __init__(
1618
data_storage_name: str,
1719
module_config: ModuleConfig,
1820
develop_data: DevelopData,
21+
finish_post_connect_event: Event,
1922
):
2023
super().__init__(
2124
tactics_agent,
@@ -25,6 +28,7 @@ def __init__(
2528
data_storage_name,
2629
module_config,
2730
develop_data,
31+
finish_post_connect_event,
2832
)
2933

3034
def precompute(self) -> None:

adf_core_python/core/component/module/abstract_module.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
from __future__ import annotations
22

3+
import time
34
from abc import ABC, abstractmethod
45
from typing import TYPE_CHECKING
56

7+
from adf_core_python.core.logger.logger import get_agent_logger
8+
69
if TYPE_CHECKING:
710
from adf_core_python.core.agent.communication.message_manager import MessageManager
811
from adf_core_python.core.agent.develop.develop_data import DevelopData
@@ -32,6 +35,10 @@ def __init__(
3235
self._count_prepare: int = 0
3336
self._count_update_info: int = 0
3437
self._count_update_info_current_time: int = 0
38+
self._logger = get_agent_logger(
39+
f"{self.__class__.__module__}.{self.__class__.__qualname__}",
40+
self._agent_info,
41+
)
3542

3643
self._sub_modules: list[AbstractModule] = []
3744

@@ -56,7 +63,11 @@ def resume(self, precompute_data: PrecomputeData) -> AbstractModule:
5663
def prepare(self) -> AbstractModule:
5764
self._count_prepare += 1
5865
for sub_module in self._sub_modules:
66+
start_time = time.time()
5967
sub_module.prepare()
68+
self._logger.debug(
69+
f"{self.__class__.__name__}'s sub_module {sub_module.__class__.__name__} prepare time: {time.time() - start_time:.3f}",
70+
)
6071
return self
6172

6273
def update_info(self, message_manager: MessageManager) -> AbstractModule:

adf_core_python/core/component/tactics/tactics_agent.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import time
34
from abc import ABC, abstractmethod
45
from typing import TYPE_CHECKING, Any, Optional
56

@@ -130,9 +131,17 @@ def module_resume(self, precompute_data: PrecomputeData) -> None:
130131

131132
def module_prepare(self) -> None:
132133
for module in self._modules:
134+
start_time = time.time()
133135
module.prepare()
136+
self._logger.debug(
137+
f"module {module.__class__.__name__} prepare time: {time.time() - start_time:.3f}",
138+
)
134139
for action in self._actions:
140+
start_time = time.time()
135141
action.prepare()
142+
self._logger.debug(
143+
f"module {action.__class__.__name__} prepare time: {time.time() - start_time:.3f}",
144+
)
136145
# for executor in self._command_executor:
137146
# executor.prepare()
138147

adf_core_python/core/launcher/agent_launcher.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def __init__(self, config: Config):
3232
self.config = config
3333
self.logger = get_logger(__name__)
3434
self.connectors: list[Connector] = []
35-
self.thread_list: list[threading.Thread] = []
35+
self.agent_thread_list: list[threading.Thread] = []
3636

3737
def init_connector(self) -> None:
3838
loader_name, loader_class_name = self.config.get_value(
@@ -63,12 +63,27 @@ def launch(self) -> None:
6363
host, port, self.logger
6464
)
6565

66+
connector_thread_list: list[threading.Thread] = []
6667
for connector in self.connectors:
6768
threads = connector.connect(component_launcher, self.config, self.loader)
68-
for thread in threads:
69-
thread.daemon = True
70-
thread.start()
71-
self.thread_list.extend(threads)
69+
self.agent_thread_list.extend(threads)
7270

73-
for thread in self.thread_list:
71+
def connect() -> None:
72+
for thread, event in threads.items():
73+
thread.daemon = True
74+
thread.start()
75+
is_not_timeout = event.wait(5)
76+
if not is_not_timeout:
77+
break
78+
79+
connector_thread = threading.Thread(target=connect)
80+
connector_thread_list.append(connector_thread)
81+
connector_thread.start()
82+
83+
for thread in connector_thread_list:
84+
thread.join()
85+
86+
self.logger.info("All agents have been launched")
87+
88+
for thread in self.agent_thread_list:
7489
thread.join()

adf_core_python/core/launcher/config_key.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ class ConfigKey:
2222
KEY_AMBULANCE_CENTRE_COUNT: Final[str] = "adf.team.office.ambulance.count"
2323
KEY_FIRE_STATION_COUNT: Final[str] = "adf.team.office.fire.count"
2424
KEY_POLICE_OFFICE_COUNT: Final[str] = "adf.team.office.police.count"
25+
# adf-core-python

adf_core_python/core/launcher/connect/component_launcher.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,12 @@ def make_connection(self) -> Connection:
1717
return Connection(self.host, self.port)
1818

1919
def connect(self, agent: Agent, _request_id: int) -> None:
20-
# self.logger.bind(agent_id=agent.get_id())
21-
2220
self.logger.info(
23-
f"{agent.__class__.__name__} connecting to {self.host}:{self.port} request_id: {_request_id}"
21+
f"{agent.__class__.__name__} trying to connect to {self.host}:{self.port} request_id: {_request_id}"
2422
)
2523
connection = self.make_connection()
2624
try:
2725
connection.connect()
28-
# ソケットが使用しているPORT番号を取得
29-
if connection.socket is not None:
30-
self.logger.info(
31-
f"Connected to {self.host}:{self.port} on port {connection.socket.getsockname()[1]}"
32-
)
3326
except socket.timeout:
3427
self.logger.warning(f"Connection to {self.host}:{self.port} timed out")
3528
return

adf_core_python/core/launcher/connect/connector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def connect(
1616
component_launcher: ComponentLauncher,
1717
config: Config,
1818
loader: AbstractLoader,
19-
) -> list[threading.Thread]:
19+
) -> dict[threading.Thread, threading.Event]:
2020
raise NotImplementedError
2121

2222
def get_connected_agent_count(self) -> int:

0 commit comments

Comments
 (0)