understanding coroutine
TRANSCRIPT
![Page 1: Understanding Coroutine](https://reader035.vdocuments.mx/reader035/viewer/2022062512/5882bcab1a28abb2478b51c9/html5/thumbnails/1.jpg)
深⼊入理解协程⼩小拿@果壳 2016.04.07
![Page 2: Understanding Coroutine](https://reader035.vdocuments.mx/reader035/viewer/2022062512/5882bcab1a28abb2478b51c9/html5/thumbnails/2.jpg)
协程 == yield ?
![Page 3: Understanding Coroutine](https://reader035.vdocuments.mx/reader035/viewer/2022062512/5882bcab1a28abb2478b51c9/html5/thumbnails/3.jpg)
⺫⽬目录• 定义与背景 (为什么产⽣生)
• 理解协程的概念 (是什么)
• 协程的没落与复兴 (历史)
• 原型实现* in C (落实到程序)
• 效率、Python与Go (为什么重要)
![Page 4: Understanding Coroutine](https://reader035.vdocuments.mx/reader035/viewer/2022062512/5882bcab1a28abb2478b51c9/html5/thumbnails/4.jpg)
协程 = coroutine
co- 英语的派⽣生词缀,能跟动词、名词、形容词相缀合,表⽰示 together, joint 等意思,也就是『⼀一起』
![Page 5: Understanding Coroutine](https://reader035.vdocuments.mx/reader035/viewer/2022062512/5882bcab1a28abb2478b51c9/html5/thumbnails/5.jpg)
定义• Coroutines are computer program components
that generalize subroutines for nonpreemptive multitasking, by allowing multiple entry points for suspending and resuming execution at certain locations.
• Coroutines are well-suited for implementing more familiar program components such as cooperative tasks, exceptions, event loop, iterators, infinite lists and pipes.
![Page 6: Understanding Coroutine](https://reader035.vdocuments.mx/reader035/viewer/2022062512/5882bcab1a28abb2478b51c9/html5/thumbnails/6.jpg)
背景
• According to Donald Knuth, the term coroutine was coined by Melvin Conway in 1958, after he applied it to construction of an assembly program.
![Page 7: Understanding Coroutine](https://reader035.vdocuments.mx/reader035/viewer/2022062512/5882bcab1a28abb2478b51c9/html5/thumbnails/7.jpg)
编译器
![Page 8: Understanding Coroutine](https://reader035.vdocuments.mx/reader035/viewer/2022062512/5882bcab1a28abb2478b51c9/html5/thumbnails/8.jpg)
理解协程的概念
![Page 9: Understanding Coroutine](https://reader035.vdocuments.mx/reader035/viewer/2022062512/5882bcab1a28abb2478b51c9/html5/thumbnails/9.jpg)
调⽤用⽅方式• subroutine: …a subroutine merely was an extension
of the computer hardware, introduced to save lines of coding. (调⽤用/被调⽤用)
• coroutine: …as a team of programs, each member of the team having a certain job to do. (协同/相互调⽤用)
• WIKI: Coroutines are computer program components that generalize subroutines for nonpreemptive multitasking, by allowing multiple entry points for suspending and resuming execution at certain locations. from TAOCP
![Page 10: Understanding Coroutine](https://reader035.vdocuments.mx/reader035/viewer/2022062512/5882bcab1a28abb2478b51c9/html5/thumbnails/10.jpg)
调度⽅方式
• 协作式调度
• 抢占式调度
• WIKI: generalize subroutines for nonpreemptive multitasking
![Page 11: Understanding Coroutine](https://reader035.vdocuments.mx/reader035/viewer/2022062512/5882bcab1a28abb2478b51c9/html5/thumbnails/11.jpg)
执⾏行流程
多边扫描算法 multipass
![Page 12: Understanding Coroutine](https://reader035.vdocuments.mx/reader035/viewer/2022062512/5882bcab1a28abb2478b51c9/html5/thumbnails/12.jpg)
• psychological difference
• time difference
• space difference
协程与多遍扫描的转换
![Page 13: Understanding Coroutine](https://reader035.vdocuments.mx/reader035/viewer/2022062512/5882bcab1a28abb2478b51c9/html5/thumbnails/13.jpg)
协程与多遍扫描的转换
• ⽣生产者/消费者模式
• 后⾯面需要前⾯面扫描的统计信息
![Page 14: Understanding Coroutine](https://reader035.vdocuments.mx/reader035/viewer/2022062512/5882bcab1a28abb2478b51c9/html5/thumbnails/14.jpg)
compare with multiple-pass algorithm
Little old lady, riding a bus. "Little boy, can you tell me how to get off at Pasadena Street?"
Little boy. "Just watch me, and get off two stops before I do."
(The joke is that the little boy gives a two-pass algorithm.)
from TAOCP
![Page 15: Understanding Coroutine](https://reader035.vdocuments.mx/reader035/viewer/2022062512/5882bcab1a28abb2478b51c9/html5/thumbnails/15.jpg)
协程的没落与复兴
![Page 16: Understanding Coroutine](https://reader035.vdocuments.mx/reader035/viewer/2022062512/5882bcab1a28abb2478b51c9/html5/thumbnails/16.jpg)
⾃自顶向下程序设计⽅方法
• top-down design ⾃自顶向下,逐步求精
• structured programming 结构化程序设计
• 过程调⽤用的⽅方式进⾏行流程控制
• 循序/选择/重复
• goto statement
![Page 17: Understanding Coroutine](https://reader035.vdocuments.mx/reader035/viewer/2022062512/5882bcab1a28abb2478b51c9/html5/thumbnails/17.jpg)
⾃自顶向下程序设计⽅方法
![Page 18: Understanding Coroutine](https://reader035.vdocuments.mx/reader035/viewer/2022062512/5882bcab1a28abb2478b51c9/html5/thumbnails/18.jpg)
Python• 迭代器 iteratior
• ⽣生成器 generator
• …
• ⽣生产者/消费者
• 多线程/竟态同步/缓冲区
• 任务轻量/线程机制过于复杂
![Page 19: Understanding Coroutine](https://reader035.vdocuments.mx/reader035/viewer/2022062512/5882bcab1a28abb2478b51c9/html5/thumbnails/19.jpg)
tornado• C10K
• high concurrency
• select/poll/epoll
• event-driven
• callback
• async IO
![Page 20: Understanding Coroutine](https://reader035.vdocuments.mx/reader035/viewer/2022062512/5882bcab1a28abb2478b51c9/html5/thumbnails/20.jpg)
async IO
![Page 21: Understanding Coroutine](https://reader035.vdocuments.mx/reader035/viewer/2022062512/5882bcab1a28abb2478b51c9/html5/thumbnails/21.jpg)
callback hell
![Page 22: Understanding Coroutine](https://reader035.vdocuments.mx/reader035/viewer/2022062512/5882bcab1a28abb2478b51c9/html5/thumbnails/22.jpg)
coroutine/yield
![Page 23: Understanding Coroutine](https://reader035.vdocuments.mx/reader035/viewer/2022062512/5882bcab1a28abb2478b51c9/html5/thumbnails/23.jpg)
原型实现* in C
![Page 24: Understanding Coroutine](https://reader035.vdocuments.mx/reader035/viewer/2022062512/5882bcab1a28abb2478b51c9/html5/thumbnails/24.jpg)
实现
让出yield / 恢复resume
![Page 25: Understanding Coroutine](https://reader035.vdocuments.mx/reader035/viewer/2022062512/5882bcab1a28abb2478b51c9/html5/thumbnails/25.jpg)
实现
![Page 26: Understanding Coroutine](https://reader035.vdocuments.mx/reader035/viewer/2022062512/5882bcab1a28abb2478b51c9/html5/thumbnails/26.jpg)
实现
![Page 27: Understanding Coroutine](https://reader035.vdocuments.mx/reader035/viewer/2022062512/5882bcab1a28abb2478b51c9/html5/thumbnails/27.jpg)
再看定义• Coroutines are computer program components
that generalize subroutines for nonpreemptive multitasking, by allowing multiple entry points for suspending and resuming execution at certain locations.
• Coroutines are well-suited for implementing more familiar program components such as cooperative tasks, exceptions, event loop, iterators, infinite lists and pipes.
![Page 28: Understanding Coroutine](https://reader035.vdocuments.mx/reader035/viewer/2022062512/5882bcab1a28abb2478b51c9/html5/thumbnails/28.jpg)
效率、Python与Go 从效率的⾓角度看协程
![Page 29: Understanding Coroutine](https://reader035.vdocuments.mx/reader035/viewer/2022062512/5882bcab1a28abb2478b51c9/html5/thumbnails/29.jpg)
Parallelism vs Concurrency
• 多线程
• 并发 Concurrency
• 并⾏行 Parallelism
Concurrency is about dealing with lots of things at once.Parallelism is about doing lots of things at once.
— Rob Pike
![Page 30: Understanding Coroutine](https://reader035.vdocuments.mx/reader035/viewer/2022062512/5882bcab1a28abb2478b51c9/html5/thumbnails/30.jpg)
Python• 单线程
• GIL (Global Interpreter Lock)
All tasks working within Python must
acquire the singular GIL to legally
process, effectively limiting all Python to
a single thread of execution.
![Page 31: Understanding Coroutine](https://reader035.vdocuments.mx/reader035/viewer/2022062512/5882bcab1a28abb2478b51c9/html5/thumbnails/31.jpg)
Go与并⾏行编程
⼀一个关键字 go
![Page 32: Understanding Coroutine](https://reader035.vdocuments.mx/reader035/viewer/2022062512/5882bcab1a28abb2478b51c9/html5/thumbnails/32.jpg)
goroutine的调度
• M: Machine (OS thread)
• P: logical Processor
• G: Goroutine
并⾏行度 runtime.GOMAXPROCS(runtime.NumCPU())
![Page 33: Understanding Coroutine](https://reader035.vdocuments.mx/reader035/viewer/2022062512/5882bcab1a28abb2478b51c9/html5/thumbnails/33.jpg)
Go与异步⾮非阻塞在Go语⾔言中,如何让⼀一个⺴⽹网络连接使⽤用⾮非阻塞I/O?怎么对⺴⽹网络连接进⾏行异步读写?
这⼏几个问题回答是:Go的⺴⽹网络程序不使⽤用异步操作,⼀一切操作都是同步的,每个goroutine处理⼀一个连接。 在Go代码层⾯面,开发者看到的是使⽤用goroutine来进⾏行阻塞式读写,⽽而在Go的内部实现中,则是利⽤用异步操作,通过对goroutine的调度来完成对事件的处理。作为最⼩小的调度单位,goroutine之间是并发(可能⾮非并⾏行)执⾏行的。
『以同步的形式表达了异步的⾏行为』
写程序 = ⽤用程序语⾔言表达意图
![Page 34: Understanding Coroutine](https://reader035.vdocuments.mx/reader035/viewer/2022062512/5882bcab1a28abb2478b51c9/html5/thumbnails/34.jpg)
协程带来了什么
Go通过协程统⼀一了异步、并发与并⾏行的表达
Python通过协程利⽤用了异步带来的效率提升 Go通过协程利⽤用了异步、并⾏行带来的效率提升
![Page 35: Understanding Coroutine](https://reader035.vdocuments.mx/reader035/viewer/2022062512/5882bcab1a28abb2478b51c9/html5/thumbnails/35.jpg)
goroutine vs coroutine在其他语⾔言中,⽐比如 Python、Lua或者C#中都有协程的概念。goroutine 可以看做 coroutine 在 Go 中的实现,不过与通常意义的 coroutine 还是有两点不同:
• goroutine 意味着并⾏行;coroutine ⼀一般不是
• goroutine 通过 channel 来通信;coroutine 通过让出yield 和恢复 resume 操作来通信
![Page 36: Understanding Coroutine](https://reader035.vdocuments.mx/reader035/viewer/2022062512/5882bcab1a28abb2478b51c9/html5/thumbnails/36.jpg)
参考Reference:
1 https://en.wikipedia.org/wiki/Coroutine https://zh.wikipedia.org/wiki/%E5%8D%8F%E7%A8%8B↩ 2 Donald Knuth, The Art of Computer Programming, Volume 1. Addison-Wesley, ISBN 0-201-89683-4. Section
1.4.2 describes coroutines in the “pure” form.↩ 3 http://geek.csdn.net/news/detail/49827 计算机语⾔言协程的历史、现在和未来↩ 4 http://blog.chinaunix.net/uid-17299695-id-3059110.html epoll的⾼高效实现原理, https://www.zhihu.com/
question/20122137↩ 5 http://yanyiwu.com/work/2014/12/20/c-coroutine.html 谈谈并发编程中的协程↩ 6 http://demo.pythoner.com/itt2zh/ch5.html 异步Web请求↩ 7 http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html Simon Tatham, Coroutines in C↩ 8 http://searchsoftwarequality.techtarget.com/definition/structured-programming-modular-programming
structured programming(modular programming)↩ 9 https://segmentfault.com/a/1190000003063859 Linux IO模式及 select、poll、epoll详解↩ 10http://www.goinggo.net/2014/01/concurrency-goroutines-and-gomaxprocs.html Concurrency, Goroutines
and GOMAXPROCS↩ 11http://www.cnblogs.com/yjf512/archive/2012/07/19/2599304.html Golang runtime 浅析↩ 12https://www.zhihu.com/question/20862617 golang的goroutine是如何实现的↩ 13https://talks.golang.org/2012/waza.slide Concurrency is not Parallelism↩ 14http://ifeve.com/cpp-concurrency-vs-parallel/ C++11并发编程指南↩ 15http://wiki.jikexueyuan.com/project/the-way-to-go/14.1.html 并发、并⾏行和协程↩