diff --git a/flask_app/general/llm/qianwen_plus.py b/flask_app/general/llm/qianwen_plus.py index 864e9e8..aaf63a8 100644 --- a/flask_app/general/llm/qianwen_plus.py +++ b/flask_app/general/llm/qianwen_plus.py @@ -149,5 +149,6 @@ if __name__ == "__main__": "偏离":"所有参数需在技术响应偏离表内响应,如应答有缺项,且无有效证明材料的,评标委员会有权不予认可,视同负偏离处理" } """ - res = qianwen_plus(user_query1) - print(res) \ No newline at end of file + res,a = qianwen_plus(user_query1,True) + print(res) + print(a) \ No newline at end of file diff --git a/flask_app/general/llm/大模型通用函数.py b/flask_app/general/llm/大模型通用函数.py index 90e86c8..472a001 100644 --- a/flask_app/general/llm/大模型通用函数.py +++ b/flask_app/general/llm/大模型通用函数.py @@ -9,7 +9,7 @@ import requests from flask_app.general.读取文件.clean_pdf import extract_common_header, clean_page_content @sleep_and_retry -@limits(calls=10, period=1) # 每秒最多调用20次,qpm=1200万,两个服务器分流,每个10 +@limits(calls=9, period=1) # 每秒最多调用20次,qpm=1200万,两个服务器分流,每个10 //吃满可能有问题,改为9 def rate_limiter(): pass # 这个函数本身不执行任何操作,只用于限流 diff --git a/flask_app/test_case/test_函数并发限制.py b/flask_app/test_case/test_函数并发限制.py index c6f9357..5ff27a9 100644 --- a/flask_app/test_case/test_函数并发限制.py +++ b/flask_app/test_case/test_函数并发限制.py @@ -1,72 +1,50 @@ import time +import concurrent.futures from ratelimit import limits, sleep_and_retry -from threading import Thread from functools import wraps - -# 定义共享的限流器:每秒最多调用4次 +# 共享限流器:每秒最多允许 4 次调用 @sleep_and_retry @limits(calls=4, period=1) def rate_limiter(): - pass # 此函数仅用于限流控制,不执行任何操作 + current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + print(f"[限流器] {current_time} - Rate limiter invoked") + pass # 此函数仅用于限流控制 -# 创建一个共享的装饰器,在被装饰函数 (func) 执行之前,先调用 rate_limiter()。 +# 创建一个共享装饰器,在被装饰函数执行之前先调用 rate_limiter() def shared_rate_limit(func): @wraps(func) def wrapper(*args, **kwargs): - rate_limiter() # 应用共享的限流器 + rate_limiter() # 先调用限流器 return func(*args, **kwargs) - return wrapper - +# 被装饰的测试函数,每次调用会输出调用编号和当前时间 @shared_rate_limit -def limited_test_func(counter, thread_name): - print(f"Thread {thread_name} - limited_test_func called {counter}") - return f"Thread {thread_name} - limited_test_func result {counter}" +def test_function(index): + current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + print(f"[测试函数] {current_time} - Test function {index} executed") + return current_time - -@shared_rate_limit -def another_limited_func(counter, thread_name): - print(f"Thread {thread_name} - another_limited_func called {counter}") - return f"Thread {thread_name} - another_limited_func result {counter}" - - -def thread_function(thread_name): - for i in range(1, 21): # 每个线程序号从1到20 - # 先调用 limited_test_func - try: - result = limited_test_func(counter=i, thread_name=thread_name) - print(result) - except Exception as e: - print(f"Thread {thread_name} - limited_test_func Error: {e}") - print("超时") - - # 然后调用 another_limited_func - try: - result = another_limited_func(counter=i, thread_name=thread_name) - print(result) - except Exception as e: - print(f"Thread {thread_name} - another_limited_func Error: {e}") - print("超时") - -#1s执行4次,每个线程执行20*2次,5个线程。预计耗时50s -if __name__ == "__main__": - # 定义多个线程,每个线程负责调用两个函数1到20的数字 - threads = [] - num_threads = 5 # 启动5个线程 +def main(): start_time = time.time() + print("开始测试:", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(start_time))) + num_calls = 100 - for thread_id in range(num_threads): - thread_name = f"Thread-{thread_id + 1}" - thread = Thread(target=thread_function, args=(thread_name,)) - threads.append(thread) - thread.start() - - # 等待所有线程完成 - for thread in threads: - thread.join() + # 使用线程池模拟并发执行 100 次调用 + with concurrent.futures.ThreadPoolExecutor(max_workers=100) as executor: + futures = [executor.submit(test_function, i) for i in range(num_calls)] + # 等待所有任务完成 + results = [future.result() for future in concurrent.futures.as_completed(futures)] end_time = time.time() - print("所有线程完成。") - print("总共耗时: {:.2f} 秒".format(end_time - start_time)) + duration = end_time - start_time + + print("\n测试结束") + print("开始时间:", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(start_time))) + print("结束时间:", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(end_time))) + print(f"总执行时间: {duration:.2f} 秒") + print("预期:由于限流规则每秒执行 4 次,100 个调用预计需要约 25 秒。") + +if __name__ == '__main__': + main()