【python】异步IO
No1:
协程看上去也是子程序,但执行过程中,在子程序内部可中断,然后转而执行别的子程序,在适当的时候再返回来接着执行。
优势:
1.最大的优势就是协程极高的执行效率。因为子程序切换不是线程切换,而是由程序自身控制,因此,没有线程切换的开销,和多线程比,线程数量越多,协程的性能优势就越明显。
2.不需要多线程的锁机制,因为只有一个线程,也不存在同时写变量冲突,在协程中控制共享资源不加锁,只需要判断状态就好了,所以执行效率比多线程高很多。
No2:
因为协程是一个线程执行,那怎么利用多核CPU呢?最简单的方法是多进程+协程,既充分利用多核,又充分发挥协程的高效率,可获得极高的性能。
Python对协程的支持是通过generator实现的。
- def consumer():
- r = ''
- while True:
- n = yield r
- if not n:
- return
- print('[CONSUMER] Consuming %s...' % n)
- r = '200 OK'
- def produce(c):
- c.send(None)
- n = 0
- while n < 5:
- n = n + 1
- print('[PRODUCER] Producing %s...' % n)
- r = c.send(n)
- print('[PRODUCER] Consumer return: %s' % r)
- c.close()
- c = consumer()
- produce(c)
运行结果
consumer
函数是一个generator
,把一个consumer
传入produce
后:
首先调用
c.send(None)
启动生成器;然后,一旦生产了东西,通过
c.send(n)
切换到consumer
执行;consumer
通过yield
拿到消息,处理,又通过yield
把结果传回;produce
拿到consumer
处理的结果,继续生产下一条消息;produce
决定不生产了,通过c.close()
关闭consumer
,整个过程结束。
整个流程无锁,由一个线程执行,produce
和consumer
协作完成任务,所以称为“协程”,而非线程的抢占式多任务
No3:
【asyncio】
- import asyncio
- async def hello():
- print("Hello world!")
- r=await asyncio.sleep(1)
- print("Hello again!")
- loop=asyncio.get_event_loop()
- loop.run_until_complete(hello())
- loop.close()
运行结果
- import threading
- import asyncio
- @asyncio.coroutine
- def hello():
- print('Hello world! (%s)' % threading.currentThread())
- yield from asyncio.sleep(1)
- print('Hello again! (%s)' % threading.currentThread())
- loop=asyncio.get_event_loop()
- tasks=[hello(),hello()]
- loop.run_until_complete(asyncio.wait(tasks))
- loop.close()
运行结果
- import asyncio
- @asyncio.coroutine
- def wget(host):
- print('wget %s...' % host)
- connect=asyncio.open_connection(host,80)
- reader,writer=yield from connect
- header='GET / HTTP/1.0\r\nHost:%s\r\n\r\n' % host
- writer.write(header.encode('utf-8'))
- yield from writer.drain()
- while True:
- line = yield from reader.readline()
- if line == b'\r\n':
- break
- print('%s header > %s' % (host,line.decode('utf-8').rstrip()))
- writer.close()
- loop=asyncio.get_event_loop()
- tasks=[wget(host) for host in ['www.sina.com.cn','www.sohu.com','www.163.com']]
- loop.run_until_complete(asyncio.wait(tasks))
- loop.close()
运行结果
No4:
异步操作需要在coroutine
中通过yield from
完成;
多个coroutine
可以封装成一组Task然后并发执行。
No5:
async
和await
是针对coroutine的新语法,要使用新的语法,只需要做两步简单的替换:
- 把
@asyncio.coroutine
替换为async
; - 把
yield from
替换为await
。
No6:
【aiohttp】
- import asyncio
- from aiohttp import web
- async def index(request):
- await asyncio.sleep(0.5)
- return web.Response(body=b'<h1>Index</h1>', content_type='text/html')
- async def hello(request):
- await asyncio.sleep(0.5)
- text='<h1>hello,%s!</h1>' % request.match_info['name']
- return web.Response(body=text.encode('utf-8'), content_type='text/html')
- async def init(loop):
- app=web.Application(loop=loop)
- app.router.add_route('GET','/',index)
- app.router.add_route('GET','/hello/{name}',hello)
- srv=await loop.create_server(app.make_handler(),'127.0.0.1',8000)
- print('Server started at http://127.0.0.1:8000...')
- return srv
- loop=asyncio.get_event_loop()
- loop.run_until_complete(init(loop))
- loop.run_forever()
运行结果
【python】异步IO的更多相关文章
- Python异步IO --- 轻松管理10k+并发连接
前言 异步操作在计算机软硬件体系中是一个普遍概念,根源在于参与协作的各实体处理速度上有明显差异.软件开发中遇到的多数情况是CPU与IO的速度不匹配,所以异步IO存在于各种编程框架中,客户端比如浏览 ...
- python异步IO编程(一)
python异步IO编程(一) 基础概念 协程:python generator与coroutine 异步IO (async IO):一种由多种语言实现的与语言无关的范例(或模型). asyncio ...
- python异步IO编程(二)
python异步IO编程(二) 目录 开门见山 Async IO设计模式 事件循环 asyncio 中的其他顶层函数 开门见山 下面我们用两个简单的例子来让你对异步IO有所了解 import asyn ...
- Python - 异步IO\数据库\队列\缓存
协程 协程,又称微线程,纤程.英文名Coroutine.一句话说明什么是线程:协程是一种用户态的轻量级线程,协程一定是在单线程运行的. 协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和 ...
- Python异步IO
在IO操作的过程中,当前线程被挂起,而其他需要CPU执行的代码就无法被当前线程执行了. 我们可以使用多线程或者多进程来并发执行代码,为多个用户服务. 但是,一旦线程数量过多,CPU的时间就花在线程切换 ...
- python -- 异步IO 协程
python 3.4 >>> import asyncio >>> from datetime import datetime >>> @asyn ...
- Python异步IO之协程(一):从yield from到async的使用
引言:协程(coroutine)是Python中一直较为难理解的知识,但其在多任务协作中体现的效率又极为的突出.众所周知,Python中执行多任务还可以通过多进程或一个进程中的多线程来执行,但两者之中 ...
- Python 异步IO、IO多路复用
事件驱动模型 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...
- python 异步IO
参考链接:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143208573 ...
- python 异步IO(syncio) 协程
python asyncio 网络模型有很多中,为了实现高并发也有很多方案,多线程,多进程.无论多线程和多进程,IO的调度更多取决于系统,而协程的方式,调度来自用户,用户可以在函数中yield一个状态 ...
随机推荐
- headless&unittest
为什么要使用 headless 测试? headless broswer 可以给测试带来显著好处: 对于 UI 自动化测试,少了真实浏览器加载 css,js 以及渲染页面的工作.无头测试要比真实浏览器 ...
- vuecli3初尝试(转载)
https://segmentfault.com/a/1190000016423943 在vue-cli3中安装element-ui 其中两种方式自己选择 格式化代码 使用yarn lint命令自动格 ...
- C# Parallel并发执行相关问题
1.Parallel并发执行 using System;using System.Collections.Generic;using System.Linq;using System.Text;usi ...
- vue中引入css文件
两种方式引入css文件,一种是直接在main.js中引入(也可以在其他的.vue文件中的<script></script>标签中),即下面这种写法: import 'eleme ...
- C++ lstrlen()
关于lstrlen function,参考:https://msdn.microsoft.com/en-us/library/windows/desktop/ms647492(v=vs.85).asp ...
- 该问题是需要导包!!!需要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 ...
- OpenCV-Python入门教程2-打开摄像头
一.打开摄像头 import cv2 # 打开摄像头并灰度化显示 capture = cv2.VideoCapture(0) while(True): # 获取一帧 ret, frame = capt ...
- windows下KafkaOffsetMonitor下载及安装
KafkaOffsetMonitor是一个可视化工具的jar包,如KafkaOffsetMonitor-assembly-0.2.1.jar,用来来监控kafka的使用状态. 一.下载地址 https ...
- Android NDK开发调试
ndk-stack: https://developer.android.com/ndk/guides/ndk-stack?hl=zh-cn JNI开发: https://developer.andr ...
- curl请求https请求
function curl_https($url,$data){ $ch = curl_init (); curl_setopt ( $ch, CURLOPT_URL, $url ); curl_se ...