python 多线程并发
记录下最近用的python多线程,防止丢失
path = r"E:\os\os'
names = os.listdir(path)
processor = 6 # 固定为6个进程
p = Pool(processor)
result = []
for i in range(processor):
result.append(p.apply_async(err_rec, args=(names, i, processor, path,)))
p.close()
p.join()
if len(result):
for res in result:
err_all = err_all.append(res.get()[0])
sum_data_length = sum_data_length + res.get()[1]
num_yc = num_yc + res.get()[2]
return err_all, sum_data_length, num_yc
def err_rec(Name, k, processor, path):
size = math.ceil(len(Name) / processor)
left = size * k
right = (k + 1) * size if (k + 1) * size <= len(Name) else len(Name)
names = Name[left:right]
for name in names:
....
return err_all, sum_data_length, num_yc