asyncio:异步IO、事件循环和并发
asyncio模块提供了使用协程构建并发程序的工具。threading模块通过多线程实现并发,multiprocessing模块通过系统进程实现并发,asyncio则使用单线程单进程在程序中明确的时间点切换任务来实现并发。大多数情况下是在程序阻塞等待读取数据或者写入数据时发生任务切换的,但是asyncio还支持定时调度任务,能过让一个协程等待其他的完成,处理系统信号,等等功能。
异步并发的概念
使用协程处理多任务
- 启动协程
 - 协程返回值
 - 协程链
 - 生成器替代协程
 
定时调度常规函数
- 计划调度“Soon”
 - 延迟调度
 - 特定时间调度
 
异步生产结果
- 等待Future
 - Future回调
 
并发执行Tasks
- 启动Task
 - 取消Task
 - 从协程创建Task
 
用控制结构组建协程
- 等待多个协程
 - 从协程聚集结果
 - 操作完成时进行处理
 
同步原语
- Locks
 - Events
 - Conditions
 - Queues
 
协议抽象类的异步I/O
- Echo Server
 - Echo Client
 - 输出
 - 其他实例
 
使用协程和流的异步I/O
- Echo Server
 - Echo Client
 - 输出
 - 其他实例
 
使用SSL
与DNS服务进行交互
- 按名称查询地址
 - 按地址查询名称
 
使用子进程
- 使用协议抽象子进程
 - 使用协程和流子进程
 - 发送数据到子进程
 
接收Unix信号
协程结合线程和进程
- 线程
 - 进程
 
调试asyncio
注意
在Python 3.5中,asyncio仍然是一个临时模块,这个API在Python 3.6中稳定了,大部分的变化被反向移植到了Python 3.5的补丁版本。因此,在不同版本的Python 3.5下,该模块可能会略有不同。
参考资料
- Standard library documentation for asyncio
 - PEP 3156 – Asynchronous IO Support Rebooted: the “asyncio” Module
 - PEP 380 – Syntax for Delegating to a Subgenerator
 - PEP 492 – Coroutines with async and await syntax
 - concurrent.futures – Manage Pools of Concurrent Tasks
 - socket – Low-level network communication
 - select – Low-level asynchronous I/O tools
 - socketserver – Framework for creating network servers
 - asyncio: What’s New in Python 3.6 – Summary of the changes to asyncio as the API stablized in Python 3.6.
 - trollius – A port of Tulip, the original version of asyncio, to Python 2.
 - The New asyncio Module in Python 3.4: Event Loops – Article by Gastón Hillar in Dr. Dobb’s
 - Exploring Python 3’s Asyncio by Example – Blog post by Chat Lung
 - A Web Crawler With asyncio Coroutines – An article in The Architecture of Open Source Applications by A. Jesse Jiryu Davis and Guido van Rossum
 - Playing with asyncio – blog post by Nathan Hoad
 - Async I/O and Python – blog post by Mark McLoughlin
 - A Curious Course on Coroutines and Concurrency – PyCon 2009 tutorial by David Beazley
 - How the heck does async/await work in Python 3.5? – blog post by Brett Cannon
 - Unix Network Programming, Volume 1: The Sockets Networking API, 3/E By W. Richard Stevens, Bill Fenner, and Andrew M. Rudoff. Published by Addison-Wesley Professional, 2004. ISBN-10: 0131411551
 - Foundations of Python Network Programminng, 3/E By Brandon Rhodes and John Goerzen. Published by Apress, 2014. ISBN-10: 1430258543