一般的程序都是顺序执行,资源消耗开销大,我们可使用多线程方式加速,以下为例,仅供参考研究:
"python创建多线程"
import requests
import threading
import time
urls = [
f'https://www.cnblogs.com/#p{page}'
for page in range(1,50+1)
]
def craw(url):
response = requests.get(url)
print(url,len(response.text))
def single_thread():
print("single_thread begin!")
for url in urls:
craw(url)
print("single_thread end!")
def multi_thread():
print("multi_thread begin!")
threads = []
for url in urls:
threads.append(
threading.Thread(target=craw,args=(url,))
)
# 线程开始
for thread in threads:
thread.start()
# 线程等待
for thread in threads:
thread.join()
print("multi_thread end!")
if __name__ =="__main__":
start = time.time()
single_thread()
end = time.time()
print("single thread:",end - start)
start = time.time()
multi_thread()
end = time.time()
print("multi_thread:", end - start)
单线程时间结果
多线程时间结果: