Skip to content

Commit 3e6d234

Browse files
authored
Merge pull request #1215 from Steve-Dusty/sequential-workflow
Add async execution support to AgentRearrange and fix SequentialWorkflow initialization tests
2 parents cab4d42 + a59e8c8 commit 3e6d234

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

swarms/structs/agent_rearrange.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22
from concurrent.futures import ThreadPoolExecutor
33
from typing import Any, Callable, Dict, List, Optional, Union
4-
4+
import asyncio
55
from swarms.structs.agent import Agent
66
from swarms.structs.conversation import Conversation
77
from swarms.structs.multi_agent_exec import run_agents_concurrently
@@ -908,6 +908,45 @@ def concurrent_run(
908908
except Exception as e:
909909
self._catch_error(e)
910910

911+
async def run_async(
912+
self,
913+
task: str,
914+
img: Optional[str] = None,
915+
*args,
916+
**kwargs,
917+
) -> Any:
918+
"""
919+
Asynchronously executes a task through the agent workflow.
920+
921+
This method enables asynchronous execution of tasks by running the
922+
synchronous run method in a separate thread using asyncio.to_thread.
923+
This is ideal for integrating the agent workflow into async applications
924+
or when you want non-blocking execution.
925+
926+
Args:
927+
task (str): The task to be executed through the agent workflow.
928+
img (Optional[str]): Optional image input for the task. Defaults to None.
929+
*args: Additional positional arguments passed to the run method.
930+
**kwargs: Additional keyword arguments passed to the run method.
931+
932+
Returns:
933+
Any: The result of the task execution, format depends on output_type setting.
934+
935+
Raises:
936+
Exception: If an error occurs during task execution.
937+
938+
Note:
939+
This method uses asyncio.to_thread to run the synchronous run method
940+
asynchronously, allowing integration with async/await patterns.
941+
"""
942+
943+
try:
944+
return await asyncio.to_thread(
945+
self.run, task=task, img=img, *args, **kwargs
946+
)
947+
except Exception as e:
948+
self._catch_error(e)
949+
911950
def _serialize_callable(
912951
self, attr_value: Callable
913952
) -> Dict[str, Any]:

tests/structs/test_sequential_workflow.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,6 @@
33
from swarms import Agent, SequentialWorkflow
44

55

6-
# Test SequentialWorkflow class
7-
def test_sequential_workflow_initialization():
8-
workflow = SequentialWorkflow()
9-
assert isinstance(workflow, SequentialWorkflow)
10-
assert len(workflow.tasks) == 0
11-
assert workflow.max_loops == 1
12-
assert workflow.autosave is False
13-
assert (
14-
workflow.saved_state_filepath
15-
== "sequential_workflow_state.json"
16-
)
17-
assert workflow.restore_state_filepath is None
18-
assert workflow.dashboard is False
19-
206

217
def test_sequential_workflow_initialization_with_agents():
228
"""Test SequentialWorkflow initialization with agents"""

0 commit comments

Comments
 (0)