From 452474ff50175087bdfad9555c3e99b5795fd99d Mon Sep 17 00:00:00 2001 From: Myungjin Lee Date: Mon, 5 Aug 2024 14:57:55 -0700 Subject: [PATCH] feat: boolean function to check if world is broken A broken world exception is raised for collective operations such as send and recv. However, if those operations are not called and runtime error is not raised, the broken world won't be noticed until any of the operations is called. To check if the world is broken or not without calling the operations, a new function is introduced. --- multiworld/world_communicator.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/multiworld/world_communicator.py b/multiworld/world_communicator.py index a57fae3..50d6ffb 100644 --- a/multiworld/world_communicator.py +++ b/multiworld/world_communicator.py @@ -76,11 +76,11 @@ def __del__(self): """Cleanup the class instance.""" del self._broken_world - def add_world(self, world_name): + def add_world(self, world_name) -> None: """Add a new world to the world comm manager.""" self._broken_world[world_name] = False - def remove_world(self, world_name): + def remove_world(self, world_name) -> None: """Remove a world from the world comm manager.""" logger.debug(f"remove world {world_name}") try: @@ -88,6 +88,20 @@ def remove_world(self, world_name): except KeyError: pass + def is_broken(self, world_name: str) -> bool: + """Return true if the given world is broken; otherwise return false. + + A world is considered broken if no key for the world name is found. + + Args: + world_name: name of world + + Returns: + A boolean value to indicate whether a world is broken or not. + """ + logger.debug(f"check if world {world_name} is broken") + return self._broken_world.get(world_name, True) + async def _wait_work(self, work: Work, world_name: str) -> None: """Do busy-waiting for work to be done.