码迷,mamicode.com
首页 > 其他好文 > 详细

协程概念

时间:2019-03-05 19:55:51      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:切换   which   python   edit   component   eval   hand   parallel   ide   

 关键词:协程 栈帧 指令 切换 跳转

 

异步结果的等待、后继计算的保存。 

 

Coroutines are computer program components that generalize subroutines for non-preemptive multitasking, by allowing execution to be suspended and resumed. Coroutines are well-suited for implementing familiar program components such as cooperative tasks, exceptions, event loops, iterators, infinite lists and pipes.

 

Python 3.5 introduces explicit support for coroutines with async/await syntax 

 

Comparison with threads[edit]

Coroutines are very similar to threads. However, coroutines are cooperatively multitasked, whereas threads are typically preemptively multitasked. This means that coroutines provide concurrency but not parallelism. T

 

var q := new queue

 

coroutine produce

    loop

        while q is not full

            create some new items

            add the items to q

        yield to consume

 

coroutine consume

    loop

        while q is not empty

            remove some items from q

            use the items

        yield to produce

 

Implementations for C[edit]

In order to implement general-purpose coroutines, a second call stack must be obtained, which is a feature not directly supported by the C language. 

 

 

Implementations for Rust[edit]

There is a library for Rust that provides coroutines.[45] Generators are an experimental feature available in nightly rust that provides an implementation of coroutines with async/await.[46]

 

协程概念

标签:切换   which   python   edit   component   eval   hand   parallel   ide   

原文地址:https://www.cnblogs.com/feng9exe/p/10479161.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!