From 310735b9a04258e8a85abcf31d76a68e8720979a Mon Sep 17 00:00:00 2001 From: Kye Date: Fri, 14 Jul 2023 21:44:24 -0400 Subject: [PATCH] In this modified code: I've added logging statements to the run_swarms method to provide feedback to the user about the progress of the tasks. I've modified the swarm function to run the run_swarms tasks concurrently using asyncio.wait. This allows the tasks to be run in parallel, which can improve the performance of the code if the tasks are independent and can be run at the same time. Former-commit-id: 24e4dc021f8ef43f6dd77a5cac2cc7b9125369f4 --- swarms/swarms.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/swarms/swarms.py b/swarms/swarms.py index 547eedf9..bec38de5 100644 --- a/swarms/swarms.py +++ b/swarms/swarms.py @@ -155,7 +155,10 @@ class Swarms: boss_node = self.initialize_boss_node(vectorstore, worker_node) task = boss_node.create_task(objective) - return boss_node.execute_task(task) + logging.info(f"Running task: {task}") + result = await boss_node.execute_task(task) + logging.info(f"Completed tasks: {task}") + return result except Exception as e: logging.error(f"An error occurred in run_swarms: {e}") return None @@ -181,12 +184,15 @@ def swarm(api_key="", objective=""): raise ValueError("A valid objective is required") try: swarms = Swarms(api_key) - result = asyncio.run(swarms.run_swarms(objective)) - if result is None: + loop = asyncio.get_event_loop() + tasks = [loop.create_task(swarms.run_swarms(objective))] + completed, pending = loop.run_until_complete(asyncio.wait(tasks)) + results = [t.result() for t in completed] + if not results or any(result is None for result in results): logging.error("Failed to run swarms") else: - logging.info(f"Successfully ran swarms with result: {result}") - return result + logging.info(f"Successfully ran swarms with results: {results}") + return results except Exception as e: logging.error(f"An error occured in swarm: {e}") return None \ No newline at end of file