Skip to content

Commit 7ea87ef

Browse files
committed
fix: Update connector methods to return a dictionary of threads and events
1 parent 13c9aab commit 7ea87ef

File tree

16 files changed

+92
-27
lines changed

16 files changed

+92
-27
lines changed

adf_core_python/core/agent/agent.py

Lines changed: 12 additions & 1 deletion
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
@@ -293,9 +297,16 @@ def handler_sense(self, msg: Any) -> None:
293297
].intValue,
294298
)
295299
)
296-
300+
start_marge_time = _time.time()
297301
self.world_model.merge(change_set)
302+
end_marge_time = _time.time()
303+
self.logger.debug(
304+
f"Time to merge: {end_marge_time - start_marge_time:.2f} seconds"
305+
)
298306
self.update_step_info(time, change_set, heard_commands)
307+
self.logger.info(
308+
f"Time to update_step_info: {_time.time() - end_marge_time:.2f} seconds"
309+
)
299310

300311
def send_acknowledge(self, request_id: int) -> None:
301312
ak_ack = AKAcknowledge()

adf_core_python/core/agent/platoon/platoon.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import time
2+
from threading import Event
3+
14
from adf_core_python.core.agent.action.action import Action
25
from adf_core_python.core.agent.agent import Agent
36
from adf_core_python.core.agent.config.module_config import ModuleConfig
@@ -19,6 +22,7 @@ def __init__(
1922
data_storage_name: str,
2023
module_config: ModuleConfig,
2124
develop_data: DevelopData,
25+
finish_post_connect_event: Event,
2226
) -> None:
2327
super().__init__(
2428
is_precompute,
@@ -28,6 +32,7 @@ def __init__(
2832
data_storage_name,
2933
module_config,
3034
develop_data,
35+
finish_post_connect_event,
3136
)
3237
self._tactics_agent = tactics_agent
3338
self._team_name = team_name
@@ -83,6 +88,10 @@ def post_connect(self) -> None:
8388
case Mode.PRECOMPUTED:
8489
pass
8590
case Mode.NON_PRECOMPUTE:
91+
start_time = time.time()
92+
self._logger.info(
93+
f"Prepare start {self._agent_info.get_entity_id().get_value()}"
94+
)
8695
self._tactics_agent.prepare(
8796
self._agent_info,
8897
self._world_info,
@@ -91,6 +100,7 @@ def post_connect(self) -> None:
91100
self.precompute_data,
92101
self._develop_data,
93102
)
103+
self._logger.info(f"Prepare time: {time.time() - start_time:.3f} sec")
94104

95105
def think(self) -> None:
96106
action: Action = self._tactics_agent.think(

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: 10 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_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,9 @@ 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_logger(
39+
f"{self.__class__.__module__}.{self.__class__.__qualname__}",
40+
)
3541

3642
self._sub_modules: list[AbstractModule] = []
3743

@@ -56,7 +62,11 @@ def resume(self, precompute_data: PrecomputeData) -> AbstractModule:
5662
def prepare(self) -> AbstractModule:
5763
self._count_prepare += 1
5864
for sub_module in self._sub_modules:
65+
start_time = time.time()
5966
sub_module.prepare()
67+
self._logger.info(
68+
f"module {sub_module.__class__.__name__} prepare time: {time.time() - start_time:.3f}",
69+
)
6070
return self
6171

6272
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.info(
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.info(
143+
f"action {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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import importlib
22
import threading
3+
import time
34

45
from adf_core_python.core.component.abstract_loader import AbstractLoader
56
from adf_core_python.core.config.config import Config
@@ -68,6 +69,7 @@ def launch(self) -> None:
6869
for thread in threads:
6970
thread.daemon = True
7071
thread.start()
72+
time.sleep(0.5)
7173
self.thread_list.extend(threads)
7274

7375
for thread in self.thread_list:

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:

adf_core_python/core/launcher/connect/connector_ambulance_center.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ def connect(
2424
component_launcher: ComponentLauncher,
2525
config: Config,
2626
loader: AbstractLoader,
27-
) -> list[threading.Thread]:
27+
) -> dict[threading.Thread, threading.Event]:
2828
count: int = config.get_value(ConfigKey.KEY_AMBULANCE_CENTRE_COUNT, 0)
2929
if count == 0:
30-
return []
30+
return {}
3131

32-
threads: list[threading.Thread] = []
32+
threads: dict[threading.Thread, threading.Event] = {}
3333

3434
for _ in range(count):
3535
if loader.get_tactics_ambulance_center() is None:
@@ -54,6 +54,7 @@ def connect(
5454
)
5555

5656
request_id: int = component_launcher.generate_request_id()
57+
finish_post_connect_event = threading.Event()
5758
thread = threading.Thread(
5859
target=component_launcher.connect,
5960
args=(
@@ -70,7 +71,7 @@ def connect(
7071
),
7172
name=f"AmbulanceCenterAgent-{request_id}",
7273
)
73-
threads.append(thread)
74+
threads[thread] = finish_post_connect_event
7475

7576
self.logger.info("Connected ambulance center (count: %d)" % count)
7677
return threads

0 commit comments

Comments
 (0)