-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add delayed container start #131
Changes from 8 commits
8ab6fc1
391fe60
0b46b79
5da9746
98e5780
a087346
4553fc1
9cf064a
f601070
a080e76
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -125,7 +125,10 @@ async def start_agent_loop(): | |
|
||
for agent in container._agents.values(): | ||
agent._do_start() | ||
|
||
container.running = True | ||
container.on_ready() | ||
|
||
while not terminate_event.is_set(): | ||
await asyncio.sleep(WAIT_STEP) | ||
await container.shutdown() | ||
|
@@ -294,7 +297,7 @@ async def _execute_dispatch_event(self, event_pipe: AioDuplex): | |
except Exception: | ||
logger.exception("A dispatched coroutine has failed!") | ||
except EOFError: | ||
# other side disconnected -> task not necessry anymore | ||
# other side disconnected -> task not necessary anymore | ||
pass | ||
except Exception: | ||
logger.exception("The Dispatch Event Loop has failed!") | ||
|
@@ -363,6 +366,8 @@ def __init__( | |
self._container = container | ||
self._mp_enabled = False | ||
self._ctx = get_context("spawn") | ||
self._agent_process_init_list = [] | ||
self._started = False | ||
|
||
def _init_mp(self): | ||
# For agent multiprocessing support | ||
|
@@ -444,6 +449,21 @@ def _find_sp_queue(self, aid): | |
raise ValueError(f"The aid '{aid}' does not exist in any subprocess.") | ||
|
||
def create_agent_process(self, agent_creator, container, mirror_container_creator): | ||
# dill must be dumped on creation, otherwise the later variable state would be stored | ||
agent_creator = dill.dumps(agent_creator) | ||
mirror_container_creator = dill.dumps(mirror_container_creator) | ||
if not self._started: | ||
self._agent_process_init_list.append( | ||
(agent_creator, container, mirror_container_creator) | ||
) | ||
else: | ||
self.create_internal_agent_process( | ||
maurerle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
agent_creator, container, mirror_container_creator | ||
) | ||
|
||
def create_internal_agent_process( | ||
self, agent_creator, container, mirror_container_creator | ||
): | ||
if not self._active: | ||
self._init_mp() | ||
self._active = True | ||
|
@@ -461,8 +481,8 @@ def create_agent_process(self, agent_creator, container, mirror_container_creato | |
clock=container.clock, | ||
kwargs=container._kwargs, | ||
), | ||
dill.dumps(agent_creator), | ||
dill.dumps(mirror_container_creator), | ||
agent_creator, | ||
mirror_container_creator, | ||
to_pipe_message, | ||
self._main_queue, | ||
to_pipe, | ||
|
@@ -510,6 +530,18 @@ def handle_message_in_sp(self, message, receiver_id, priority, meta): | |
else: | ||
sp_queue_of_agent.put_nowait((priority, message, meta)) | ||
|
||
async def start(self): | ||
if not self._started: | ||
self._started = True | ||
for ( | ||
agent_creator, | ||
container, | ||
mirror_container_creator, | ||
) in self._agent_process_init_list: | ||
await self.create_internal_agent_process( | ||
agent_creator, container, mirror_container_creator | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. gather did not work somehow, as the ProcessHandle is unhashable.. |
||
|
||
async def shutdown(self): | ||
if self._active: | ||
# send a signal to all sub processes to terminate their message feed in's | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regarding the design, it makes sense that delayed starting agent processes is an additional feature. I do not feel the prior approach was bad. Being able to start the process at will before starting the container makes sense as it is closer to the normal agent lifecycle. So I would suggest to introduce a new method for delayed agent process start (or a flag?). This also would prevent breaking the API.