No1:

协程看上去也是子程序,但执行过程中,在子程序内部可中断,然后转而执行别的子程序,在适当的时候再返回来接着执行。

优势:

1.最大的优势就是协程极高的执行效率。因为子程序切换不是线程切换,而是由程序自身控制,因此,没有线程切换的开销,和多线程比,线程数量越多,协程的性能优势就越明显。

2.不需要多线程的锁机制,因为只有一个线程,也不存在同时写变量冲突,在协程中控制共享资源不加锁,只需要判断状态就好了,所以执行效率比多线程高很多。

No2:

因为协程是一个线程执行,那怎么利用多核CPU呢?最简单的方法是多进程+协程,既充分利用多核,又充分发挥协程的高效率,可获得极高的性能。

Python对协程的支持是通过generator实现的。

  1. def consumer():
  2. r = ''
  3. while True:
  4. n = yield r
  5. if not n:
  6. return
  7. print('[CONSUMER] Consuming %s...' % n)
  8. r = '200 OK'
  9.  
  10. def produce(c):
  11. c.send(None)
  12. n = 0
  13. while n < 5:
  14. n = n + 1
  15. print('[PRODUCER] Producing %s...' % n)
  16. r = c.send(n)
  17. print('[PRODUCER] Consumer return: %s' % r)
  18. c.close()
  19.  
  20. c = consumer()
  21. produce(c)

运行结果

consumer函数是一个generator,把一个consumer传入produce后:

  1. 首先调用c.send(None)启动生成器;

  2. 然后,一旦生产了东西,通过c.send(n)切换到consumer执行;

  3. consumer通过yield拿到消息,处理,又通过yield把结果传回;

  4. produce拿到consumer处理的结果,继续生产下一条消息;

  5. produce决定不生产了,通过c.close()关闭consumer,整个过程结束。

整个流程无锁,由一个线程执行,produceconsumer协作完成任务,所以称为“协程”,而非线程的抢占式多任务

No3:

【asyncio】

  1. import asyncio
  2.  
  3. async def hello():
  4. print("Hello world!")
  5. r=await asyncio.sleep(1)
  6. print("Hello again!")
  7.  
  8. loop=asyncio.get_event_loop()
  9. loop.run_until_complete(hello())
  10. loop.close()

运行结果

  1. import threading
  2. import asyncio
  3.  
  4. @asyncio.coroutine
  5. def hello():
  6. print('Hello world! (%s)' % threading.currentThread())
  7. yield from asyncio.sleep(1)
  8. print('Hello again! (%s)' % threading.currentThread())
  9.  
  10. loop=asyncio.get_event_loop()
  11. tasks=[hello(),hello()]
  12. loop.run_until_complete(asyncio.wait(tasks))
  13. loop.close()

运行结果

  1. import asyncio
  2.  
  3. @asyncio.coroutine
  4. def wget(host):
  5. print('wget %s...' % host)
  6. connect=asyncio.open_connection(host,80)
  7. reader,writer=yield from connect
  8. header='GET / HTTP/1.0\r\nHost:%s\r\n\r\n' % host
  9. writer.write(header.encode('utf-8'))
  10. yield from writer.drain()
  11. while True:
  12. line = yield from reader.readline()
  13. if line == b'\r\n':
  14. break
  15. print('%s header > %s' % (host,line.decode('utf-8').rstrip()))
  16. writer.close()
  17.  
  18. loop=asyncio.get_event_loop()
  19. tasks=[wget(host) for host in ['www.sina.com.cn','www.sohu.com','www.163.com']]
  20. loop.run_until_complete(asyncio.wait(tasks))
  21. loop.close()

运行结果

No4:

异步操作需要在coroutine中通过yield from完成;

多个coroutine可以封装成一组Task然后并发执行。

No5:

asyncawait是针对coroutine的新语法,要使用新的语法,只需要做两步简单的替换:

  1. @asyncio.coroutine替换为async
  2. yield from替换为await

No6:

【aiohttp】

  1. import asyncio
  2.  
  3. from aiohttp import web
  4.  
  5. async def index(request):
  6. await asyncio.sleep(0.5)
  7. return web.Response(body=b'<h1>Index</h1>', content_type='text/html')
  8.  
  9. async def hello(request):
  10. await asyncio.sleep(0.5)
  11. text='<h1>hello,%s!</h1>' % request.match_info['name']
  12. return web.Response(body=text.encode('utf-8'), content_type='text/html')
  13.  
  14. async def init(loop):
  15. app=web.Application(loop=loop)
  16. app.router.add_route('GET','/',index)
  17. app.router.add_route('GET','/hello/{name}',hello)
  18. srv=await loop.create_server(app.make_handler(),'127.0.0.1',8000)
  19. print('Server started at http://127.0.0.1:8000...')
  20. return srv
  21.  
  22. loop=asyncio.get_event_loop()
  23. loop.run_until_complete(init(loop))
  24. loop.run_forever()

运行结果

【python】异步IO的更多相关文章

  1. Python异步IO --- 轻松管理10k+并发连接

    前言   异步操作在计算机软硬件体系中是一个普遍概念,根源在于参与协作的各实体处理速度上有明显差异.软件开发中遇到的多数情况是CPU与IO的速度不匹配,所以异步IO存在于各种编程框架中,客户端比如浏览 ...

  2. python异步IO编程(一)

    python异步IO编程(一) 基础概念 协程:python  generator与coroutine 异步IO (async IO):一种由多种语言实现的与语言无关的范例(或模型). asyncio ...

  3. python异步IO编程(二)

    python异步IO编程(二) 目录 开门见山 Async IO设计模式 事件循环 asyncio 中的其他顶层函数 开门见山 下面我们用两个简单的例子来让你对异步IO有所了解 import asyn ...

  4. Python - 异步IO\数据库\队列\缓存

    协程 协程,又称微线程,纤程.英文名Coroutine.一句话说明什么是线程:协程是一种用户态的轻量级线程,协程一定是在单线程运行的. 协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和 ...

  5. Python异步IO

    在IO操作的过程中,当前线程被挂起,而其他需要CPU执行的代码就无法被当前线程执行了. 我们可以使用多线程或者多进程来并发执行代码,为多个用户服务. 但是,一旦线程数量过多,CPU的时间就花在线程切换 ...

  6. python -- 异步IO 协程

    python 3.4 >>> import asyncio >>> from datetime import datetime >>> @asyn ...

  7. Python异步IO之协程(一):从yield from到async的使用

    引言:协程(coroutine)是Python中一直较为难理解的知识,但其在多任务协作中体现的效率又极为的突出.众所周知,Python中执行多任务还可以通过多进程或一个进程中的多线程来执行,但两者之中 ...

  8. Python 异步IO、IO多路复用

    事件驱动模型 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  9. python 异步IO

    参考链接:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143208573 ...

  10. python 异步IO(syncio) 协程

    python asyncio 网络模型有很多中,为了实现高并发也有很多方案,多线程,多进程.无论多线程和多进程,IO的调度更多取决于系统,而协程的方式,调度来自用户,用户可以在函数中yield一个状态 ...

随机推荐

  1. headless&unittest

    为什么要使用 headless 测试? headless broswer 可以给测试带来显著好处: 对于 UI 自动化测试,少了真实浏览器加载 css,js 以及渲染页面的工作.无头测试要比真实浏览器 ...

  2. vuecli3初尝试(转载)

    https://segmentfault.com/a/1190000016423943 在vue-cli3中安装element-ui 其中两种方式自己选择 格式化代码 使用yarn lint命令自动格 ...

  3. C# Parallel并发执行相关问题

    1.Parallel并发执行 using System;using System.Collections.Generic;using System.Linq;using System.Text;usi ...

  4. vue中引入css文件

    两种方式引入css文件,一种是直接在main.js中引入(也可以在其他的.vue文件中的<script></script>标签中),即下面这种写法: import 'eleme ...

  5. C++ lstrlen()

    关于lstrlen function,参考:https://msdn.microsoft.com/en-us/library/windows/desktop/ms647492(v=vs.85).asp ...

  6. 该问题是需要导包!!!需要pom中添加依赖The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application

    <!-- https://mvnrepository.com/artifact/org.apache.taglibs/taglibs-standard-impl --><depend ...

  7. OpenCV-Python入门教程2-打开摄像头

    一.打开摄像头 import cv2 # 打开摄像头并灰度化显示 capture = cv2.VideoCapture(0) while(True): # 获取一帧 ret, frame = capt ...

  8. windows下KafkaOffsetMonitor下载及安装

    KafkaOffsetMonitor是一个可视化工具的jar包,如KafkaOffsetMonitor-assembly-0.2.1.jar,用来来监控kafka的使用状态. 一.下载地址 https ...

  9. Android NDK开发调试

    ndk-stack: https://developer.android.com/ndk/guides/ndk-stack?hl=zh-cn JNI开发: https://developer.andr ...

  10. curl请求https请求

    function curl_https($url,$data){ $ch = curl_init (); curl_setopt ( $ch, CURLOPT_URL, $url ); curl_se ...