53 lines
1.9 KiB
Python
53 lines
1.9 KiB
Python
import concurrent.futures
|
||
import os
|
||
|
||
from flask_app.general.通义千问long import qianwen_long
|
||
|
||
|
||
def multi_threaded_calls(file_id, user_query, num_threads=10):
|
||
"""
|
||
使用多线程同时调用 qianwen_long 函数。
|
||
参数:
|
||
- file_id: 上传文件的 ID
|
||
- user_query: 用户查询
|
||
- num_threads: 并发线程数量(默认 10)
|
||
"""
|
||
results = []
|
||
|
||
# 定义一个辅助函数来调用 qianwen_long 并返回结果
|
||
def call_function(thread_id):
|
||
print(f"线程 {thread_id} 开始调用 qianwen_long")
|
||
result = qianwen_long(file_id, user_query)
|
||
# print(f"线程 {thread_id} 调用完成,结果长度:{len(result)}")
|
||
return result
|
||
|
||
# 使用 ThreadPoolExecutor 来管理线程池
|
||
with concurrent.futures.ThreadPoolExecutor(max_workers=num_threads) as executor:
|
||
# 提交所有任务
|
||
futures = {executor.submit(call_function, i + 1): i + 1 for i in range(num_threads)}
|
||
|
||
# 处理完成的任务
|
||
for future in concurrent.futures.as_completed(futures):
|
||
thread_id = futures[future]
|
||
try:
|
||
data = future.result()
|
||
results.append(data)
|
||
print(f"线程 {thread_id} 完成,返回数据:{data[:50]}...") # 只打印前50个字符
|
||
except Exception as exc:
|
||
print(f"线程 {thread_id} 生成异常:{exc}")
|
||
|
||
return results
|
||
|
||
|
||
if __name__ == "__main__":
|
||
# 定义参数
|
||
# file_path = r"C:\Users\Administrator\Desktop\货物标\截取test\2-招标文件_before.pdf"
|
||
# file_id = upload_file(file_path)
|
||
file_id = "file-fe-ah0F0SJUY2cEzPx5nONRBvwd"
|
||
user_query = "项目名称是?"
|
||
num_threads = 200 # 并发线程数量
|
||
# 执行多线程调用
|
||
responses = multi_threaded_calls(file_id, user_query, num_threads)
|
||
# 打印所有响应
|
||
for idx, response in enumerate(responses, start=1):
|
||
print(f"响应 {idx}: {response}") |