学习笔记
今天尝试一下多线程与多进程的使用(多个参数的使用),做下笔记,
一、多线程
from concurrent.futures import ThreadPoolExecutor
def loof_func(参数1,参数2,参数3,参数4):
# 需要执行的语句 略
def main():
names=['','','','']
rise = 0.1
startTime = '2021-12-31 23:58:00'
endTime = '2023-01-01 23:59:59'
item_list = [] # 组装参数 形成 [[],[],[]] 结构
for name in names:
temp = [] #临时组装数据用
temp.append(rise)
temp.append(startTime)
temp.append(endTime)
temp.append(name)
item_list.append(temp)
with ThreadPoolExecutor() as pool: # 建立线程池
for i in range(len(item_list)):
pool.submit(loop_func,*item_list[i]) # 组合循环执行
if __name__ == '__main__':
main()
二、多进程
from concurrent.futures import ProcessPoolExecutor
def loof_func(参数1,参数2,参数3,参数4):
# 需要执行的语句 略
def main():
names=['','','','']
rise = 0.1
startTime = '2021-12-31 23:58:00'
endTime = '2023-01-01 23:59:59'
item_list = [] # 组装参数 形成 [[],[],[]] 结构
for name in names:
temp = [] #临时组装数据用
temp.append(rise)
temp.append(startTime)
temp.append(endTime)
temp.append(name)
item_list.append(temp)
pool = ProcessPoolExecutor(max_workers=16) # 进程数量
for i in range(len(item_list)):
pool.submit(loop_func,*item_list[i])
pool.shutdown()
if __name__ == '__main__':
main()
记录一下,也欢迎大佬指正。