2.12 使用turbo作为plus超限时的保底选择

This commit is contained in:
zy123 2025-02-12 16:16:15 +08:00
parent 0388beaeaa
commit 2a25c28e20
3 changed files with 34 additions and 55 deletions

View File

@ -149,5 +149,6 @@ if __name__ == "__main__":
"偏离":"所有参数需在技术响应偏离表内响应,如应答有缺项,且无有效证明材料的,评标委员会有权不予认可,视同负偏离处理"
}
"""
res = qianwen_plus(user_query1)
res,a = qianwen_plus(user_query1,True)
print(res)
print(a)

View File

@ -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 # 这个函数本身不执行任何操作,只用于限流

View File

@ -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()