-
Notifications
You must be signed in to change notification settings - Fork 935
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Executor] Support node_concurrency when running an async flow (#1698)
# Description Use asyncio.semaphore to support concurrency control. When node_concurrency is set to 2: sync_passthrough1 and async_passthrough1 can run concurrently: ![1705287434459](https://github.com/microsoft/promptflow/assets/39176492/487f1d03-34fa-4016-bf01-ff31d64d2c3f) When node_concurrency is set to 1: Only one of sync_passthrough1 and async_passthrough1 can at the same time: ![1705288060087](https://github.com/microsoft/promptflow/assets/39176492/9ce31cd9-4af4-4451-a898-06cccf5c2321) --------- Co-authored-by: Min Shi <[email protected]>
- Loading branch information
Showing
2 changed files
with
55 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import os | ||
import pytest | ||
from promptflow.executor import FlowExecutor | ||
from ..utils import get_flow_folder, get_yaml_file | ||
|
||
|
||
@pytest.mark.e2etest | ||
class TestAsync: | ||
@pytest.mark.parametrize( | ||
"folder_name, concurrency_levels, expected_concurrency", | ||
[ | ||
("async_tools", [1, 2, 3], [1, 2, 2]), | ||
("async_tools_with_sync_tools", [1, 2, 3], [1, 2, 2]), | ||
], | ||
) | ||
def test_executor_node_concurrency(self, folder_name, concurrency_levels, expected_concurrency): | ||
os.chdir(get_flow_folder(folder_name)) | ||
executor = FlowExecutor.create(get_yaml_file(folder_name), {}) | ||
|
||
def calculate_max_concurrency(flow_result): | ||
timeline = [] | ||
api_calls = flow_result.run_info.api_calls[0]["children"] | ||
for api_call in api_calls: | ||
timeline.append(("start", api_call["start_time"])) | ||
timeline.append(("end", api_call["end_time"])) | ||
timeline.sort(key=lambda x: x[1]) | ||
current_concurrency = 0 | ||
max_concurrency = 0 | ||
for event, _ in timeline: | ||
if event == "start": | ||
current_concurrency += 1 | ||
max_concurrency = max(max_concurrency, current_concurrency) | ||
elif event == "end": | ||
current_concurrency -= 1 | ||
return max_concurrency | ||
|
||
for i in range(len(concurrency_levels)): | ||
concurrency = concurrency_levels[i] | ||
flow_result = executor.exec_line({"input_str": "Hello"}, node_concurrency=concurrency) | ||
max_concurrency = calculate_max_concurrency(flow_result) | ||
assert max_concurrency == expected_concurrency[i] | ||
assert max_concurrency <= concurrency |