Skip to content

Commit

Permalink
add lock to prevent duplicated _make_pre_act_value calls
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 672884115
Change-Id: I6cbf875bcaea8f6e798fd68a07e3fafc9c7477d3
  • Loading branch information
jagapiou authored and copybara-github committed Sep 10, 2024
1 parent b06e0d0 commit 326ec85
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions concordia/components/agent/action_spec_ignored.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"""A component that ignores the action spec in the `pre_act` method."""

import abc
import threading
from typing import Final

from concordia.typing import entity as entity_lib
from concordia.typing import entity_component
Expand All @@ -36,7 +38,8 @@ class ActionSpecIgnored(
def __init__(self, pre_act_key: str):
super().__init__()
self._pre_act_value: str | None = None
self._pre_act_key = pre_act_key
self._pre_act_key: Final[str] = pre_act_key
self._lock: threading.Lock = threading.Lock()

@abc.abstractmethod
def _make_pre_act_value(self) -> str:
Expand All @@ -62,13 +65,13 @@ def get_pre_act_value(self) -> str:
"`POST_ACT` phase. The entity is currently in the "
f"{self.get_entity().get_phase()} phase.")

if self._pre_act_value is None:
self._pre_act_value = self._make_pre_act_value()
return self._pre_act_value
with self._lock:
if self._pre_act_value is None:
self._pre_act_value = self._make_pre_act_value()
return self._pre_act_value

def get_pre_act_key(self) -> str | None:
"""Returns the key used as a prefix in the string returned by `pre_act`.
"""
def get_pre_act_key(self) -> str:
"""Returns the key used as a prefix in the string returned by `pre_act`."""
return self._pre_act_key

def pre_act(
Expand All @@ -79,7 +82,8 @@ def pre_act(
return f"{self.get_pre_act_key()}: {self.get_pre_act_value()}"

def update(self) -> None:
self._pre_act_value = None
with self._lock:
self._pre_act_value = None

def get_named_component_pre_act_value(self, component_name: str) -> str:
"""Returns the pre-act value of a named component of the parent entity."""
Expand Down

0 comments on commit 326ec85

Please sign in to comment.