当前位置:   article > 正文

python多进程(multiprocessing)(map)_multiprocessing map

multiprocessing map
map的基本使用:
map函数一手包办了序列操作,参数传递和结果保存等一系列的操作。
from multiprocessing.dummy import Pool
poop = Pool(4)       # 4代表电脑是多少核的
results = pool.map(爬取函数,网址列表)
  • 1
  • 2
  • 3
  • 4
  • 5
from multiprocessing.dummy import Pool as ThreadPool
import requests
import time

kv = {'user-agent':'Mozilla/5.0'}

def getsource(url):
    html = requests.get(url,headers=kv)

urls = []

for i in range(0,41):
    i = i*50
    newpage = 'https://tieba.baidu.com/f?kw=读书&ie=utf-8&pn=' + str(i)
    urls.append(newpage)

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

# 多线程爬取
pool = ThreadPool(8)
time3 = time.time()
results  = pool.map(getsource, urls)
pool.close()
pool.join()
time4 = time.time()
print('多线程所消耗时间:' + str(time4 - time3))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

example

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/74899
推荐阅读
相关标签
  

闽ICP备14008679号