Python单线程与并行

分别使用单线程与并行方式对百度贴吧进行请求并记录时间

#-*-coding:utf8-*-
from multiprocessing.dummy import Pool as ThreadPool
import requests
import time

def getsource(url):
    html = requests.get(url)

urls = []

for i in range(1,31):
    newpage = 'https://tieba.baidu.com/p/7966421072?pn='+str(i)
    urls.append(newpage)

time1 = time.time()

for i in urls:
    print(i)
    getsource(i)
time2 = time.time()
print('单线程耗时:'+ str(time2-time1))

#使用并行化处理
pool = ThreadPool(4)
time3 = time.time()
results = pool.map(getsource,urls)
pool.close()
pool.join()
time4 = time.time()
print
print('并行耗时:'+ str(time4-time3))
posted @ 2022-08-09 23:19  Ryan-liang  阅读(54)  评论(0编辑  收藏  举报