浏览代码

Concurrent calls to the Wenxin model, and the exception problem when obtaining the token is fixed (#7976)

Co-authored-by: puqs1 <puqs1@lenovo.com>
wochuideng 7 月之前
父节点
当前提交
f6b9982c23
共有 1 个文件被更改,包括 7 次插入5 次删除
  1. 7 5
      api/core/model_runtime/model_providers/wenxin/_common.py

+ 7 - 5
api/core/model_runtime/model_providers/wenxin/_common.py

@@ -79,11 +79,13 @@ class BaiduAccessToken:
             # if access token not in cache, request it
             token = BaiduAccessToken(api_key)
             baidu_access_tokens[api_key] = token
-            # release it to enhance performance
-            # btw, _get_access_token will raise exception if failed, release lock here to avoid deadlock
-            baidu_access_tokens_lock.release()
-            # try to get access token
-            token_str = BaiduAccessToken._get_access_token(api_key, secret_key)
+            try:
+                # try to get access token
+                token_str = BaiduAccessToken._get_access_token(api_key, secret_key)
+            finally:
+                # release it to enhance performance
+                # btw, _get_access_token will raise exception if failed, release lock here to avoid deadlock
+                baidu_access_tokens_lock.release()
             token.access_token = token_str
             token.expires = now + timedelta(days=3)
             return token