python threading线程数
import threading
import time
name_list = [
{"李四1": 1234556},
{"李四2": 1234556},
{"李四3": 1234556},
{"李四4": 1234556},
{"李四5": 1234556},
{"李四6": 1234556},
{"李四7": 1234556},
{"李四8": 1234556},
{"李四9": 1234556},
]
batch_size = len(name_list) // 3 # 计算每个批次的条数,这里我们选择将任务分为3个批次
def process_data(data):
# 这里写处理数据的代码,例如打印数据
for i in data:
for name, value in i.items():
print(f"Processing {name}: {value}")
# 使用线程池并行执行每个批次的任务
threads = []
for i in range(batch_size):
# 从name_list中提取当前批次的条目(列表切片)
batch = name_list[i * batch_size:(i + 1) * batch_size]
# 创建线程并提交给线程池执行
thread = threading.Thread(target=process_data, args=(batch,))
threads.append(thread)
print('------------------------------')
print(time.time())
thread.start()
# 等待所有线程执行完毕
for thread in threads:
thread.join()
案例:
import math
import random
import threading
import time
import random
import requests
import script_marketing.public.config_ini as rc
from public_new import *
api_code = rc.user_basic["ppdlk"]["apicode"]
app_key = rc.user_basic["ppdlk"]["appkeyDev"] # Dev是预发
data_list = []
count = 10
max_workers = 3
for i in range(0,count):
li = {
"cell":'149' + str(random.randint(10000000,99999999)),
"custNum":'zdh' + str(random.randint(1000000000,9999999900)),
"name":"通标标准上传自动化",
"id":"c295d9b8314b4bc2672c2921e6447da9",
"registerDate":"",
"reserveField1":{
"userType":'1',
"orgName":"通标标准上传自动化"
}
}
data_list.append(li)
# 计算向上取余数,比如10条分成3个批次完成,那就是10/3=4
batch_size = math.ceil(len(data_list) / max_workers)
def process_data(data):
# 这里写处理数据的代码,例如打印数据
upload_json = {
"requestId":createUuid(),
"taskId":'zdhdd' + str(random.randint(10000000,99999999)),
"dataItems":data
}
request_upload = getData(apicodes=api_code,appkeys=app_key,jsondata=upload_json)
result = requests.post(url=rc.upload_url,json=request_upload,verify=False)
print(result.text)
threads = []
for i in range(batch_size):
# 从name_list中提取当前批次的条目(列表切片) 按照并发得数量切片max_workers
batch = data_list[i * max_workers:(i + 1) * max_workers]
# 创建线程并提交给线程池执行
thread = threading.Thread(target=process_data,args=(batch,))
threads.append(thread)
time.sleep(5)
print('------------------------------')
print(time.time())
thread.start()
# 等待所有线程执行完毕
for thread in threads:
thread.join()
结果