网址:http://www.baidu.com
代码如下:
from get_html import download
import time
import threading
url = 'http://www.baidu.com'
#普通请求
def test_1():
start = time.perf_counter()
for i in range(10):
html = download(url=url)
print('程序用时',time.perf_counter()-start)
#多线程
def test_2():
start = time.perf_counter()
tlist = list()
for i in range(10):
t = threading.Thread(target=download,args=(url,))#args=(url,)传入的是一个元组
tlist.append(t)
#列表推导式tlist = [threading.Thread(target=download,args=(url,)) for i in range(10)]
[t.start() for t in tlist] #多线程程的运行 无序运行,只需要最后结果
[t.join() for t in tlist] #等到线程执行完之后 再回到主线程中去
print('程序用时', time.perf_counter() - start)
test_1()
test_2()