5. 定义
• 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.
6. 背景
• According to Donald Knuth, the term coroutine
was coined by Melvin Conway in 1958, after he
applied it to construction of an assembly
program.
9. 调⽤用⽅方式
• 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
14. 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
27. 再看定义
• 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.
29. 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
30. 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.