不理解单线程 与 多线程?敲几遍就好了
一、单线程 与 多线程 比较
from threading import Thread
import threading
import requests
import time
#请求任务
def request_task(url):
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'}
requests.get(url,headers=headers)
time.sleep(0.5)
#获取线程名
thread_name = threading.current_thread().name
print(thread_name,'get',url)
#单线程
def singal_thread():
# 时间戳,距离1970年多少秒
start = time.time()
url = 'https://www.baidu.com/s?wd={}'
# 发送10个请求
for i in range(10):
request_task(url=url.format(i)) #调用
print('单线程耗时',time.time() - start)
#多线程
def multi_thread():
# 加入时间戳
start = time.time()
url = 'https://www.baidu.com/s?wd={}'
#线程池
thread_pool = []
for