首页  >  问答  >  正文

网页爬虫 - python的多进程怎么配合requests

这是单进程顺序执行的代码:

import requests,time,os,random

def img_down(url):
    with open("{}".format(str(random.random())+os.path.basename(url)),"wb") as fob:
        fob.write(requests.get(url).content)

urllist=[]
with open("urllist.txt","r+") as u:
    for a in u.readlines():
        urllist.append(a.strip())

s=time.clock()
for i in range(len(urllist)):
    img_down(urllist[i])
e=time.clock()

print ("time: %d" % (e-s))

这是多进程的代码:

from multiprocessing import Pool
import requests,os,time,random

def img_down(url):
    with open("{}".format(str(random.random())+os.path.basename(url)),"wb") as fob:
        fob.write(requests.get(url).content)

if __name__=="__main__":
    urllist=[]
    with open("urllist.txt","r+") as urlfob:
        for s in urlfob.readlines():
            urllist.append(s.strip())

    s=time.clock()
    p=Pool()
    for i in range(len(urllist)):
        p.apply_async(img_down,args=(urllist[i],))
    p.close()
    p.join()
    e=time.clock()
    
    print ("time: {}".format(e-s))

但是单进程和多进程花费的时间几乎没区别,问题大概是requests阻塞IO,请问理解的对不对,代码该怎么修改达到多进程的目的?
谢谢!

阿神阿神2676 天前727

全部回复(2)我来回复

  • phpcn_u1582

    phpcn_u15822017-06-22 11:54:30

    写文件的瓶颈在磁盘IO,并不在CPU,你并行并没有多大作用,你可以试试不要写入文件再对比时间

    回复
    0
  • 怪我咯

    怪我咯2017-06-22 11:54:30

    Pool 不带参数的话 是采用
    os.cpu_count() or 1
    如果是单核CPU,或者采集不到数量 就只有1个进程而已。

    应该是这个原因。

    回复
    0
  • 取消回复