python async
理解 python 的 async:
1. 协程后面的技术也是 IO 多路复用, Nginx 和 nodejs 都充分利用了这种机制.
2. Flask 线程中为什么不能直接使用标准库 asyncio 来支持异步?
asyncio 一定要有应该eventloop, 而这个 eventloop 必须运行在主线程, 不能运行在子线程中, 所以在Flask 线程中不能直接使用标准库 asyncio.
3. 如果在单并发的情况下, web响应很慢. 改成 async模式, 并不会改善"单次"请求响应速度. 当然改成 async 通常来讲还是有意义的, 因为小并发时候响应很差, 当并发量稍微增加时, 在同步模式下, 响应就会急剧变差, 甚至无法响应. async 模式下, 从小并发到大并发, 响应速度下降非常缓慢.
如何将Python的同步方法改写异步? Python的异步方法大概可分三类:
1. 专门的基于 asyncio 优化的单线程异步方法
优点: 这类方法使用简单, 就像同步方法调用一样, 并发性能很高, 即使是在大并发下, latency也不会变差.
缺点: 基于 asyncio 的类库比较少, 最有名的是 aiohttp 库, https://github.com/aio-libs 还有一些.
实现一个这样的方法, 有些难度, 可以参考 aio-libs 下的一些类库.
2. 基于多线程(多进程)的 awaitable 方法
优点: 这类方法使用简单, 就像同步方法调用一样. 另外, 编写这类方法也非常简单, 先先一个同步方法, 然后利用一个 wrapper 就能转成异步方法.
缺点: 性能较差, 主要是因为没有做过细节优化, 只有方法整体运行完毕后, 才会将控制权交出, 管控粒度太粗, 通过多线程来支持并发.
3. 传统的多线程(多进程)的后台任务
优点: 如果不需要加上回调功能, 使用这种方法非常简单, 代码也容易维护, 并发性能也很好.
缺点: 要及时获取这种后台任务的结果, 只能使用回调函数, 代码就变得难以维护.
说明:多线程(多进程)推荐使用 cocurrent.futures.ThreadPoolExecutor 和 cocurrent.futures.ProcessPoolExecutor 两个池.
理解Python Async
https://www.aeracode.org/2018/02/19/python-async-simplified/
https://stackabuse.com/overview-of-async-io-in-python-3-7/
https://pymotw.com/3/asyncio/index.html
https://www.blog.pythonlibrary.org/2016/07/26/python-3-an-intro-to-asyncio/
https://tryexceptpass.org/article/asyncio-in-37/
https://tryexceptpass.org/article/controlling-python-async-creep/
https://tryexceptpass.org/article/threaded-asynchronous-magic-and-how-to-wield-it/
基于 asyncio 的 Aiohttp web 框架, 比如适合作 restful api 服务
Aiohttp 和 Flask 的对比
https://stackabuse.com/asynchronous-python-for-web-development/
https://stackabuse.com/asynchronous-vs-synchronous-python-performance-analysis/
python中重要的模块--asyncio
https://www.cnblogs.com/zhaof/p/8490045.html
https://www.syncd.cn/article/asyncio_article_01
https://www.syncd.cn/article/asyncio_article_02
https://www.syncd.cn/article/asyncio_article_03
可以为 awaitable 的 Task 增加回调:
task.add_done_callback(got_result) # task完成的时候通知我们
Django 社区推动的 ASGI 规范和实现
ASGI 服务器和框架
https://asgi.readthedocs.io/en/latest/implementations.html
Sanic -类 flask 的异步web framework
Python黑魔法 --- 异步IO( asyncio) 协程
https://www.jianshu.com/p/b5e347b3a17c
asyncio 中的 future 其实和 cocurrent.futures 中的future 不是一回事,
https://stackoverflow.com/questions/49350346/how-to-wrap-custom-future-to-use-with-asyncio-in-python
python async的更多相关文章
- 并发 并行 进程 线程 协程 异步I/O python async
一些草率不精确的观点: 并发: 一起发生,occurence: sth that happens. 并行: 同时处理. parallel lines: 平行线.thread.join()之前是啥?落霞 ...
- Python Async/Await入门指南
转自:https://zhuanlan.zhihu.com/p/27258289 本文将会讲述Python 3.5之后出现的async/await的使用方法,以及它们的一些使用目的,如果错误,欢迎指正 ...
- Python资源大全
The Python Tutorial (Python 2.7.11) 的中文翻译版本.Python Tutorial 为初学 Python 必备官方教程,本教程适用于 Python 2.7.X 系列 ...
- python coroutine
1. Python Async/Await入门指南 2. 用 Python 3 的 async / await 做异步编程 3.
- Python 异步编程笔记:asyncio
个人笔记,不保证正确. 虽然说看到很多人不看好 asyncio,但是这个东西还是必须学的.. 基于协程的异步,在很多语言中都有,学会了 Python 的,就一通百通. 一.生成器 generator ...
- Github上的python开源项目
Python开源项目,期待大家和我们一起共同维护 github排名榜单 https://github.com/trending github搜索榜单:https://github.com/search ...
- python异步编程 (转载)
Python Async/Await入门指南 转自:https://zhuanlan.zhihu.com/p/27258289 本文将会讲述Python 3.5之后出现的async/await的使 ...
- python爬虫如何提高效率
开启线程池: 线程池 asyncio 特殊的函数 协程 任务对象 任务对象绑定 事件循环 from multiprocessing.dummy import Pool map(func,alist): ...
- Python3 ORM hacking
#!/usr/bin/env python3 # -*- coding: utf- -*- # # Python3 ORM hacking # 说明: # 之前分析了一个Python2 ORM的源代码 ...
随机推荐
- Supply Initial Data提供初始数据 (EF)
Open the Updater.cs (Updater.vb) file, located in the MySolution.Module project's Database Update fo ...
- 使用Kubernetes进行ProxySQL本机群集
自v1.4.2起,ProxySQL支持本机群集.这意味着多个ProxySQL实例可识别群集; 他们了解彼此的状态,并能够通过根据配置版本,时间戳和校验和值同步最新的配置来自动处理配置更改. Proxy ...
- ArcGIS以数据库作为数据源作为source发布服务步骤详解(以Postgresql为例)及各种发布问题
创建企业级数据库 Data Management Tools-->Geodatabase Administration-->Create Enterprise Geodatabase 按如 ...
- nRF24L01+组网方式及防撞(防冲突)机制的实战分享
利用多个nRF24L01+模块组网通信的实现方式 这里讨论的组网方式,不包含使用6个通道实现的多对1通信方式,因其只限于6个发送端,局限性很大,可以附加其他技术实现更好的组网,暂时这里不讨论.这里分享 ...
- MySql 字段分组拼接
drop table if exists T_Test; create table T_Test select 'A' parent, 'A1' child union all select 'A', ...
- 如何快速查看 group 对应的id
最近需要获取group 对应的id 数字号码,突然想不起来怎么获得了,现在在这里进行备忘一下: $ cut -d: -f3 < <(getent group sudo) getent gr ...
- 轻量级监控平台之cpu监控
轻量级监控平台之cpu监控脚本 #!/bin/bash #进程监控脚本 #功能需求: 上报机器的硬件层面-cpu负载数据 . /etc/profile . ~/.bash_profile pushur ...
- STL 中 map 的使用
涉及:vector,pair 基本结构 map 从本质上来说是一颗二叉排序树,树上的节点类型为pair.pair类型中有两个重要的成员变量,其中 first 存储了 key ,second 存储了 v ...
- ArrayList的输出以及一些问题
//首先需要创建一个ArrayList ArrayList arr=new ArrayList(); //然后往ArrayList里面插入一些值 arr.add("a"); arr ...
- jango rest-framework page_size更新
老项目是三年前写的, 这周的新项目要用上DRF的分页功能时,发现老的写法无效了. 于是看了一些文档,原来写法变了. https://blog.csdn.net/dqchouyang/article/d ...