python爬虫案列09:多线程ThreadPoolExecutor,多进程
多线程:两种方法
# # 多线程
# from threading import Thread # 导入线程类
#
#
# def fun(name):
# for i in range(100):
# print(name, i)
#
#
# if __name__ == "__main__":
# t = Thread(target=fun) # 创建一个线程类对象t,并传入参数
# t2 = Thread(target=fun, args=("我是第二个子进程",)) # 再创建一个线程类对象t2,并传入参数args为进程起个名字,必须是元组
# t.start() # start() 开始执行该线程,具体什么时候执行由CPU决定
# t2.start()
# for j in range(100):
# print("main", j)
#
# 线程池,进程池
from concurrent.futures import ThreadPoolExecutor # 导入进程池
def fn(name):
for i in range(1000):
print(name, i)
if __name__ == "__main__":
with ThreadPoolExecutor(20) as t: # 创建20个线程
for j in range(100):
t.submit(fn, name=f"线程{j}") # 告诉这20个线程去做什么,把线程的编号赋值给线程的名字
print(123)
多进程:跟多线程差不多
from multiprocessing import Process
def fun():
for i in range(100):
print("子进程", i)
if __name__ == "__main__":
p = Process(target=fun) # 创建一个线程类对象t,并传入参数
p.start() # start() 开始执行该线程,具体什么时候执行由CPU决定
for j in range(100):
print("主进程", j)