Skip to content

Commit 71f29c1

Browse files
committed
feat(model/transcription): add timeout in sync wait api
1 parent 7b249cf commit 71f29c1

6 files changed

Lines changed: 98 additions & 4 deletions

File tree

dashscope/aigc/image_generation.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ def wait(
177177
task: Union[str, ImageGenerationResponse], # type: ignore[override]
178178
api_key: str = None,
179179
workspace: str = None,
180+
wait_timeout: int = -1,
180181
**kwargs,
181182
) -> DashScopeAPIResponse:
182183
"""Wait for image(s) synthesis task to complete, and return the result.
@@ -186,11 +187,18 @@ def wait(
186187
ImageGenerationResponse return by async_call().
187188
api_key (str, optional): The api api_key. Defaults to None.
188189
workspace (str): The dashscope workspace id.
190+
wait_timeout (int, optional): The maximum seconds to wait.
191+
Default is -1 (no timeout).
189192
190193
Returns:
191194
DashScopeAPIResponse: The task result.
192195
"""
193-
response = super().wait(task, api_key, workspace=workspace)
196+
response = super().wait(
197+
task,
198+
api_key,
199+
workspace=workspace,
200+
wait_timeout=wait_timeout,
201+
)
194202
return ImageGenerationResponse.from_api_response(response)
195203

196204
@classmethod
@@ -487,6 +495,7 @@ async def wait(
487495
task: Union[str, ImageGenerationResponse], # type: ignore[override]
488496
api_key: str = None,
489497
workspace: str = None,
498+
wait_timeout: int = -1,
490499
**kwargs,
491500
) -> DashScopeAPIResponse:
492501
"""Wait for image(s) synthesis task to complete, and return the result.
@@ -496,11 +505,18 @@ async def wait(
496505
ImageGenerationResponse return by async_call().
497506
api_key (str, optional): The api api_key. Defaults to None.
498507
workspace (str): The dashscope workspace id.
508+
wait_timeout (int, optional): The maximum seconds to wait.
509+
Default is -1 (no timeout).
499510
500511
Returns:
501512
DashScopeAPIResponse: The task result.
502513
"""
503-
response = await super().wait(task, api_key, workspace=workspace)
514+
response = await super().wait(
515+
task,
516+
api_key,
517+
workspace=workspace,
518+
wait_timeout=wait_timeout,
519+
)
504520
return ImageGenerationResponse.from_api_response(response)
505521

506522
@classmethod

dashscope/aigc/image_synthesis.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,7 @@ async def wait(
739739
task: Union[str, ImageSynthesisResponse], # type: ignore[override]
740740
api_key: str = None,
741741
workspace: str = None,
742+
wait_timeout: int = -1,
742743
**kwargs,
743744
) -> ImageSynthesisResponse:
744745
"""Wait for image(s) synthesis task to complete, and return the result.
@@ -748,11 +749,18 @@ async def wait(
748749
ImageSynthesisResponse return by async_call().
749750
api_key (str, optional): The api api_key. Defaults to None.
750751
workspace (str): The dashscope workspace id.
752+
wait_timeout (int, optional): The maximum seconds to wait.
753+
Default is -1 (no timeout).
751754
752755
Returns:
753756
ImageSynthesisResponse: The task result.
754757
"""
755-
response = await super().wait(task, api_key, workspace=workspace)
758+
response = await super().wait(
759+
task,
760+
api_key,
761+
workspace=workspace,
762+
wait_timeout=wait_timeout,
763+
)
756764
return ImageSynthesisResponse.from_api_response(response)
757765

758766
@classmethod

dashscope/aigc/video_synthesis.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,7 @@ async def wait(
894894
task: Union[str, VideoSynthesisResponse], # type: ignore[override]
895895
api_key: str = None,
896896
workspace: str = None,
897+
wait_timeout: int = -1,
897898
**kwargs,
898899
) -> VideoSynthesisResponse:
899900
"""Wait for video synthesis task to complete, and return the result.
@@ -903,11 +904,18 @@ async def wait(
903904
VideoSynthesisResponse return by async_call().
904905
api_key (str, optional): The api api_key. Defaults to None.
905906
workspace (str): The dashscope workspace id.
907+
wait_timeout (int, optional): The maximum seconds to wait.
908+
Default is -1 (no timeout).
906909
907910
Returns:
908911
VideoSynthesisResponse: The task result.
909912
"""
910-
response = await super().wait(task, api_key, workspace=workspace)
913+
response = await super().wait(
914+
task,
915+
api_key,
916+
workspace=workspace,
917+
wait_timeout=wait_timeout,
918+
)
911919
return VideoSynthesisResponse.from_api_response(response)
912920

913921
@classmethod

dashscope/audio/asr/transcription.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,20 @@ def wait(
167167
task: Union[str, TranscriptionResponse], # type: ignore[override]
168168
api_key: str = None,
169169
workspace: str = None,
170+
wait_timeout: int = -1,
170171
**kwargs,
171172
) -> TranscriptionResponse:
172173
"""Poll task until the final results of transcription is obtained.
173174
174175
Args:
175176
task (Union[str, TranscriptionResponse]): The task_id or
176177
response including task_id returned from async_call().
178+
api_key (str, optional): The api_key. Defaults to None.
177179
workspace (str): The dashscope workspace id.
180+
wait_timeout (int, optional): The timeout for waiting.
181+
Defaults to -1.That means no timeout.
182+
If set to a value > 0, the task does not complete
183+
within this time, a timeout error response will be returned.
178184
179185
Returns:
180186
TranscriptionResponse: The result of batch transcription.
@@ -183,6 +189,7 @@ def wait(
183189
task,
184190
api_key=api_key,
185191
workspace=workspace,
192+
wait_timeout=wait_timeout,
186193
**kwargs,
187194
)
188195
return TranscriptionResponse.from_api_response(response)

dashscope/audio/qwen_asr/qwen_transcription.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ def wait(
127127
task: Union[str, TranscriptionResponse], # type: ignore[override]
128128
api_key: str = None,
129129
workspace: str = None,
130+
wait_timeout: int = -1,
130131
**kwargs,
131132
) -> TranscriptionResponse:
132133
"""Poll task until the final results of transcription is obtained.
@@ -135,6 +136,8 @@ def wait(
135136
task (Union[str, TranscriptionResponse]): The task_id or
136137
response including task_id returned from async_call().
137138
workspace (str): The dashscope workspace id.
139+
wait_timeout (int, optional): The maximum seconds to wait.
140+
Default is -1 (no timeout).
138141
139142
Returns:
140143
TranscriptionResponse: The result of batch transcription.
@@ -143,6 +146,7 @@ def wait(
143146
task,
144147
api_key=api_key,
145148
workspace=workspace,
149+
wait_timeout=wait_timeout,
146150
**kwargs,
147151
)
148152
return TranscriptionResponse.from_api_response(response)

dashscope/client/base_api.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ async def wait(
191191
task: Union[str, DashScopeAPIResponse],
192192
api_key: str = None,
193193
workspace: str = None,
194+
wait_timeout: int = -1,
194195
**kwargs,
195196
) -> DashScopeAPIResponse:
196197
"""Wait for async task completion and return task result.
@@ -199,6 +200,12 @@ async def wait(
199200
task (Union[str, DashScopeAPIResponse]): The task_id, or
200201
async_call response.
201202
api_key (str, optional): The api_key. Defaults to None.
203+
workspace (str, optional): The dashscope workspace id.
204+
wait_timeout (int, optional): The maximum seconds to wait
205+
for the task to complete. Default is -1, which means no
206+
timeout. When set to a value > 0, if the task does not
207+
complete within this time, a timeout error response will
208+
be returned.
202209
203210
Returns:
204211
DashScopeAPIResponse: The async task information.
@@ -208,6 +215,7 @@ async def wait(
208215
max_wait_seconds = 5
209216
increment_steps = 3
210217
step = 0
218+
start_time = time.time()
211219
while True:
212220
step += 1
213221
# we start by querying once every second, and double
@@ -217,6 +225,21 @@ async def wait(
217225
# (server side return immediately when ready)
218226
if wait_seconds < max_wait_seconds and step % increment_steps == 0:
219227
wait_seconds = min(wait_seconds * 2, max_wait_seconds)
228+
if 0 < wait_timeout <= (time.time() - start_time):
229+
logger.warning(
230+
"Wait task: %s timeout after %s seconds.",
231+
task_id,
232+
wait_timeout,
233+
)
234+
return DashScopeAPIResponse(
235+
request_id=task_id,
236+
status_code=HTTPStatus.REQUEST_TIMEOUT,
237+
code="WaitTaskTimeout",
238+
message=(
239+
f"Wait task: {task_id} timeout after "
240+
f"{wait_timeout} seconds."
241+
),
242+
)
220243
rsp = await cls._get(
221244
task_id,
222245
api_key,
@@ -600,6 +623,10 @@ def call(
600623
**kwargs,
601624
) -> DashScopeAPIResponse:
602625
"""Call service and get result."""
626+
wait_timeout = -1
627+
if "wait_timeout" in kwargs:
628+
wait_timeout = kwargs.pop("wait_timeout")
629+
603630
task_response = cls.async_call( # type: ignore[misc]
604631
*args,
605632
api_key=api_key,
@@ -610,6 +637,7 @@ def call(
610637
task_response,
611638
api_key=api_key,
612639
workspace=workspace,
640+
wait_timeout=wait_timeout,
613641
)
614642
return response
615643

@@ -767,6 +795,7 @@ def wait(
767795
task: Union[str, DashScopeAPIResponse],
768796
api_key: str = None,
769797
workspace: str = None,
798+
wait_timeout: int = -1,
770799
**kwargs,
771800
) -> DashScopeAPIResponse:
772801
"""Wait for async task completion and return task result.
@@ -775,6 +804,12 @@ def wait(
775804
task (Union[str, DashScopeAPIResponse]): The task_id, or
776805
async_call response.
777806
api_key (str, optional): The api_key. Defaults to None.
807+
workspace (str, optional): The dashscope workspace id.
808+
wait_timeout (int, optional): The maximum seconds to wait
809+
for the task to complete. Default is -1, which means no
810+
timeout. When set to a value > 0, if the task does not
811+
complete within this time, a timeout error response will
812+
be returned.
778813
779814
Returns:
780815
DashScopeAPIResponse: The async task information.
@@ -784,6 +819,7 @@ def wait(
784819
max_wait_seconds = 5
785820
increment_steps = 3
786821
step = 0
822+
start_time = time.time()
787823
while True:
788824
step += 1
789825
# we start by querying once every second, and double
@@ -794,6 +830,21 @@ def wait(
794830
# (server side return immediately when ready)
795831
if wait_seconds < max_wait_seconds and step % increment_steps == 0:
796832
wait_seconds = min(wait_seconds * 2, max_wait_seconds)
833+
if 0 < wait_timeout <= (time.time() - start_time):
834+
logger.warning(
835+
"Wait task: %s timeout after %s seconds.",
836+
task_id,
837+
wait_timeout,
838+
)
839+
return DashScopeAPIResponse(
840+
request_id=task_id,
841+
status_code=HTTPStatus.REQUEST_TIMEOUT,
842+
code="WaitTaskTimeout",
843+
message=(
844+
f"Wait task: {task_id} timeout after "
845+
f"{wait_timeout} seconds."
846+
),
847+
)
797848
rsp = cls._get(task_id, api_key, workspace=workspace, **kwargs)
798849
if rsp.status_code == HTTPStatus.OK:
799850
if rsp.output is None:

0 commit comments

Comments
 (0)