Skip to content

Commit 93c92e6

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

2 files changed

Lines changed: 57 additions & 0 deletions

File tree

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/client/base_api.py

Lines changed: 50 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,
@@ -606,10 +629,14 @@ def call(
606629
workspace=workspace,
607630
**kwargs,
608631
)
632+
wait_timeout = -1
633+
if "wait_timeout" in kwargs:
634+
wait_timeout = kwargs.pop("wait_timeout")
609635
response = cls.wait(
610636
task_response,
611637
api_key=api_key,
612638
workspace=workspace,
639+
wait_timeout=wait_timeout,
613640
)
614641
return response
615642

@@ -767,6 +794,7 @@ def wait(
767794
task: Union[str, DashScopeAPIResponse],
768795
api_key: str = None,
769796
workspace: str = None,
797+
wait_timeout: int = -1,
770798
**kwargs,
771799
) -> DashScopeAPIResponse:
772800
"""Wait for async task completion and return task result.
@@ -775,6 +803,12 @@ def wait(
775803
task (Union[str, DashScopeAPIResponse]): The task_id, or
776804
async_call response.
777805
api_key (str, optional): The api_key. Defaults to None.
806+
workspace (str, optional): The dashscope workspace id.
807+
wait_timeout (int, optional): The maximum seconds to wait
808+
for the task to complete. Default is -1, which means no
809+
timeout. When set to a value > 0, if the task does not
810+
complete within this time, a timeout error response will
811+
be returned.
778812
779813
Returns:
780814
DashScopeAPIResponse: The async task information.
@@ -784,6 +818,7 @@ def wait(
784818
max_wait_seconds = 5
785819
increment_steps = 3
786820
step = 0
821+
start_time = time.time()
787822
while True:
788823
step += 1
789824
# we start by querying once every second, and double
@@ -794,6 +829,21 @@ def wait(
794829
# (server side return immediately when ready)
795830
if wait_seconds < max_wait_seconds and step % increment_steps == 0:
796831
wait_seconds = min(wait_seconds * 2, max_wait_seconds)
832+
if 0 < wait_timeout <= (time.time() - start_time):
833+
logger.warning(
834+
"Wait task: %s timeout after %s seconds.",
835+
task_id,
836+
wait_timeout,
837+
)
838+
return DashScopeAPIResponse(
839+
request_id=task_id,
840+
status_code=HTTPStatus.REQUEST_TIMEOUT,
841+
code="WaitTaskTimeout",
842+
message=(
843+
f"Wait task: {task_id} timeout after "
844+
f"{wait_timeout} seconds."
845+
),
846+
)
797847
rsp = cls._get(task_id, api_key, workspace=workspace, **kwargs)
798848
if rsp.status_code == HTTPStatus.OK:
799849
if rsp.output is None:

0 commit comments

Comments
 (0)