| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
duckbill
7年前发布

Python进阶:理解Python中的异步IO和协程(Coroutine),并应用在爬虫中

   <p style="text-align:center"><img src="https://simg.open-open.com/show/661f10bd0225b148600a4aa86930a8ea.png"></p>    <h2>基础知识</h2>    <p>(1)什么是同步IO和异步IO,它们之间有什么区别?</p>    <p>答:举个现实例子,假设你需要打开4个不同的网站,但每个网站都比较卡。IO过程就相当于你打开网站的过程,CPU就是你的点击动作。你的点击动作很快,但是网站打开很慢。同步IO是指你每点击一个网址,都等待该网站彻底显示,才会去点击下一个网址。异步IO是指你点击完一个网址,不等对方服务器返回结果,立马新开浏览器窗口去打开另外一个网址,以此类推,最后同时等待4个网站彻底打开。很明显异步IO的效率更高。</p>    <p>(2)什么是协程,为什么要使用协程?</p>    <p>Python中解决IO密集型任务(打开多个网站)的方式有很多种,比如多进程、多线程。但理论上一台电脑中的线程数、进程数是有限的,而且进程、线程之间的切换也比较浪费时间。所以就出现了“协程”的概念。 <strong>协程允许一个执行过程A中断,然后转到执行过程B,在适当的时候再一次转回来,有点类似于多线程。</strong> 但协程有以下2个优势:</p>    <ul>     <li>协程的数量理论上可以是无限个,而且没有线程之间的切换动作,执行效率比线程高。</li>     <li>协程不需要“锁”机制,即不需要lock和release过程,因为所有的协程都在一个线程中。</li>     <li>相对于线程,协程更容易调试debug,因为所有的代码是顺序执行的。</li>    </ul>    <h2>Python中的异步IO和协程</h2>    <p>Python中的协程是通过“生成器(generator)”的概念实现的。这里引用廖雪峰Python教程中的例子,并做一点修改和“装饰”:</p>    <pre>  <code class="language-python">def consumer():         # 定义消费者,由于有yeild关键词,此消费者为一个生成器      print("[Consumer] Init Consumer ......")      r = "init ok"       # 初始化返回结果,并在启动消费者时,返回给生产者      while True:          n = yield r     # 消费者通过yield接收生产者的消息,同时返给其结果          print("[Consumer] conusme n = %s, r = %s" % (n, r))          r = "consume %s OK" % n     # 消费者消费结果,下个循环返回给生产者    def produce(c):         # 定义生产者,此时的 c 为一个生成器      print("[Producer] Init Producer ......")      r = c.send(None)    # 启动消费者生成器,同时第一次接收返回结果      print("[Producer] Start Consumer, return %s" % r)      n = 0      while n < 5:          n += 1          print("[Producer] While, Producing %s ......" % n)          r = c.send(n)   # 向消费者发送消息并准备接收结果。此时会切换到消费者执行          print("[Producer] Consumer return: %s" % r)      c.close()           # 关闭消费者生成器      print("[Producer] Close Producer ......")    produce(consumer())</code></pre>    <p>代码中添加了很详细的print语句和注释,帮助大家更好的理解。这里删除了源代码consumer中的“return”语句。如果还是不太明白,可以在编辑器中进行debug调试,一步步跟踪程序的运行过程。</p>    <p>关于异步IO,在Python3.4中可以使用asyncio标准库。该标准库支持一个时间循环模型(EventLoop),我们声明协程,然后将其加入到EventLoop中,即可实现异步IO。</p>    <p>Python中也有一个关于异步IO的很经典的HelloWorld程序(同样参考于廖雪峰教程):</p>    <pre>  <code class="language-python"># 异步IO例子:适配Python3.4,使用asyncio库  @asyncio.coroutine  def hello(index):                   # 通过装饰器asyncio.coroutine定义协程      print('Hello world! index=%s, thread=%s' % (index, threading.currentThread()))      yield from asyncio.sleep(1)     # 模拟IO任务      print('Hello again! index=%s, thread=%s' % (index, threading.currentThread()))    loop = asyncio.get_event_loop()     # 得到一个事件循环模型  tasks = [hello(1), hello(2)]        # 初始化任务列表  loop.run_until_complete(asyncio.wait(tasks))    # 执行任务  loop.close()                        # 关闭事件循环列表</code></pre>    <p>同样这里的代码添加了注释,并增加了index参数。输出currentThread的目的是演示当前程序都是在一个线程中执行的。返回结果如下:</p>    <pre>  <code class="language-python">Hello world! index=1, thread=<_MainThread(MainThread, started 14816)>  Hello world! index=2, thread=<_MainThread(MainThread, started 14816)>  Hello again! index=1, thread=<_MainThread(MainThread, started 14816)>  Hello again! index=2, thread=<_MainThread(MainThread, started 14816)></code></pre>    <p><strong>在Python3.5中引入了关于异步IO的新语法:async和await关键字。</strong></p>    <pre>  <code class="language-python"># 异步IO例子:适配Python3.5,使用async和await关键字  async def hello(index):       # 通过关键字async定义协程      print('Hello world! index=%s, thread=%s' % (index, threading.currentThread()))      await asyncio.sleep(1)     # 模拟IO任务      print('Hello again! index=%s, thread=%s' % (index, threading.currentThread()))    loop = asyncio.get_event_loop()     # 得到一个事件循环模型  tasks = [hello(1), hello(2)]        # 初始化任务列表  loop.run_until_complete(asyncio.wait(tasks))    # 执行任务  loop.close()                        # 关闭事件循环列表</code></pre>    <p>从代码中可以看出,使用async代替@asyncio.coroutine,使用await代替yield from,使得协程代码更加简洁易懂。</p>    <h2>在爬虫中使用协程实现异步IO</h2>    <p>异步IO特别适合爬虫的工作,因为爬虫中所有的请求都属于IO密集型任务,想得到比较好的爬虫效率,使用协程很重要。关于Http异步请求,建议使用 <a href="/misc/goto?guid=4959727843293957190" rel="nofollow,noindex"> aiohttp库 </a> ,一个异步的HTTP客户端/服务器框架。这里举个例子,更多用法可以参考其官方文档。</p>    <pre>  <code class="language-python">async def get(url):  async with aiohttp.ClientSession() as session:  async with session.get(url) as resp:  print(url, resp.status)  print(url, await resp.text())    loop = asyncio.get_event_loop()     # 得到一个事件循环模型  tasks = [                           # 初始化任务列表      get("http://zhushou.360.cn/detail/index/soft_id/3283370"),      get("http://zhushou.360.cn/detail/index/soft_id/3264775"),      get("http://zhushou.360.cn/detail/index/soft_id/705490")  ]  loop.run_until_complete(asyncio.wait(tasks))    # 执行任务  loop.close()                        # 关闭事件循环列表</code></pre>    <p> </p>    <p> </p>    <p>来自:https://zhuanlan.zhihu.com/p/24118476</p>    <p> </p>    
 本文由用户 duckbill 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1480905602470.html
IO Python Python开发