网站建设前期准备工作总结,中国建设银行陕西分行官方网站,wordpress js 插件开发,个人主页推荐网络爬虫#xff0c;这种io高密集型的应用由于大部分的时间在等待响应方面#xff0c;所以CPU的使用率一直不高#xff0c;速度也不快#xff0c;为了解决这些问题#xff0c;我们使用异步的方式来进行爬虫程序。串行的时候#xff0c;如果我们要爬一个网站#xff0c;那…网络爬虫这种io高密集型的应用由于大部分的时间在等待响应方面所以CPU的使用率一直不高速度也不快为了解决这些问题我们使用异步的方式来进行爬虫程序。串行的时候如果我们要爬一个网站那么我们通常都是一页的内容完成了再到下一页这样的话CPU的90%以上的时间用在了等待网页响应上面。异步的话我们可以同时发起多个请求一个请求发起了之后就不等待这个请求的响应马上发起第二个请求第三个请求......然后响应过来的内容我们再一个个进行处理这样的效率就高了很多。举个栗子首先我们搭建一个flask的服务器故意降低它的响应速度from flask import Flaskimport timeapp Flask(__name__)app.route(/)def hello_world():# 休眠三秒展示异步的速度time.sleep(3)return Hello World!if __name__ __main__:app.run(threadedTrue)首先我们使用python 3.5以上版本的async、await以及异步http请求库aiohttpimport asyncioimport timeimport aiohttpstart time.time()async def get(url):async with aiohttp.ClientSession() as session:async with session.get(url) as res:print(res.status)text await res.text()return textasync def hello():url http://127.0.0.1:5000/print(Waiting for,url)res await get(url)print(Result:,res)loop asyncio.get_event_loop()tasks [asyncio.ensure_future(hello()) for i in range(5)]loop.run_until_complete(asyncio.wait(tasks))end time.time()print(Cost time:,end-start)使用python的第三方库gevent也可以实现网络异步from gevent import monkey# 猴子补丁一定要先打不然就会报错monkey.patch_all()import geventimport requestsimport timedef get(url):print(Get from: ,url)r requests.session()res r.get(url)print(res.status_code,url,res.text)def synchronous_times(url):start time.time()for i in range(5):get(url)end time.time()print(同步执行的时间, start-end)def asynchronous_times(url):start time.time()gevent.joinall([gevent.spawn(get,url) for i in range(5)])end time.time()print(异步执行的时间, start-end)synchronous_times(http://127.0.0.1:5000/)asynchronous_times(http://127.0.0.1:5000/)以上就使用aiohttp、genvent实现了异步的网络请求。