项目中偶尔总会遇到,for循环处理数据,感觉如果数据量比较大了,这个遍历的效果就非常的低效了,下面采用线程池并行来演示下效果
新建 test.py
import time
words = ["apple", "bananan", "cake", "dumpling"] * 50
def clock(func):
def clocked(*args, **kwargs):
start = time.time()
result = func(*args, **kwargs)
end = time.time()
print("执行时间:", end - start)
return result
return clocked
@clock
def just_for():
for word in words:
print(word)
time.sleep(0.1)
from multiprocessing.dummy import Pool as ThreadPool
class process_data():
def process(self, item):
print(item, "正在并行for循环")
# time.sleep(5)
time.sleep(0.1)
@clock
def make_it(self):
pool = ThreadPool(processes=10)
pool.map(self.process, words)
pool.close()
pool.join()
def main():
process_data().make_it()
# just_for()
if __name__ == "__main__":
main()
本博客源码Github地址:
请随手给个star,谢谢!