优化了一部分代码,暂时还不够发博客。先这样放着
results = []
if selected_indices:
# 引入time模块用于延时
import time
batch_size = 100 # 每批处理100个
delay_minutes = 5 # 延迟5分钟
total_batches = (len(selected_indices) + batch_size - 1) // batch_size
for batch_idx in range(total_batches):
# 计算当前批次的起始和结束索引
start_idx = batch_idx * batch_size
end_idx = min((batch_idx + 1) * batch_size, len(selected_indices))
batch_indices = selected_indices[start_idx:end_idx]
print(f'处理批次 {batch_idx + 1}/{total_batches},共 {len(batch_indices)} 个任务')
# 使用线程池处理当前批次
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
batch_results = list(tqdm(executor.map(process_one, batch_indices),
total=len(batch_indices),
desc=f'批次 {batch_idx + 1} 处理'))
results.extend(batch_results)
# 如果不是最后一批,延迟5分钟
if batch_idx < total_batches - 1:
print(f'批次 {batch_idx + 1} 处理完成,开始延迟 {delay_minutes} 分钟...')
time.sleep(delay_minutes * 60) # 转换为秒
print('延迟结束,继续处理下一批次')
266

被折叠的 条评论
为什么被折叠?



