Skip to content

Commit

Permalink
add lock on reading/writing agents state
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 587697951
Change-Id: Icdfc1e0ffc64b443935d58574c31297eaf440909
  • Loading branch information
vezhnick authored and copybara-github committed Dec 4, 2023
1 parent d2d8c4e commit 9b75546
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions concordia/agents/basic_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import contextlib
import copy
import datetime

import threading
from concordia.associative_memory import associative_memory
from concordia.document import interactive_document
from concordia.language_model import language_model
Expand Down Expand Up @@ -86,6 +86,7 @@ def __init__(
self._update_interval = update_interval

self._under_interrogation = False
self._state_lock = threading.Lock()

self._components = {}
for comp in components:
Expand Down Expand Up @@ -166,10 +167,8 @@ def get_last_log(self):
return self._last_chain_of_thought

def state(self):
return '\n'.join(
f"{self._agent_name}'s " + (comp.name() + ':\n' + comp.state())
for comp in self._components.values()
)
with self._state_lock:
return self._state

def _maybe_update(self):
next_update = self._last_update + self._update_interval
Expand All @@ -181,6 +180,11 @@ def update(self):
with concurrent.futures.ThreadPoolExecutor() as executor:
for comp in self._components.values():
executor.submit(comp.update)
with self._state_lock:
self._state = '\n'.join(
f"{self._agent_name}'s " + (comp.name() + ':\n' + comp.state())
for comp in self._components.values()
)

def observe(self, observation: str):
if observation and not self._under_interrogation:
Expand Down

0 comments on commit 9b75546

Please sign in to comment.