问题描述
吉多·范罗苏姆,在他2014年的郁金香/ ASYNCIO :
Guido van Rossum, in his speech in 2014 on Tulip/Asyncio shows the slide:
任务VS协同程序
-
比较:
Compare:
- 解析度=从some_coroutine产量(...)
- 解析度=从任务收益率(some_coroutine(...))
任务就能取得进展,而不必等待它
Task can make progress without waiting for it
- 日志,你等待别的东西
- 即。从产量
和我完全缺少点。
从我的角度来看这两种结构是相同的:结果
我裸露的协同程序的情况下 - 它被预定的,所以任务反正创建,因为调度运行使用任务,然后协同程序调用者协同程序被暂停,直到被调用者完成的,然后成为自由继续执行。From my point of view both constructs are identical:
I case of bare coroutine - it gets scheduled, so the task is created anyways, because scheduler operates with Tasks, then coroutine caller coroutine is suspended until callee is done and then becomes free to continue execution.在任务的情况下 - 都一样 - 新的任务schduled和来电协程等待其完成
In case of Task - all the same - new task is schduled and caller coroutine waits for its completion.
那是什么美元,这两种情况下产生怎样的影响它的开发人员应该在实践中考虑?执行C $ c。在这样的差异
What is the difference in the way that code executed in both cases and what impact it has that developer should consider in practice?
P.S。结果
链接权威人士(GVR,政治公众人物,文档,开发者的核心笔记)将是非常美联社preciated。p.s.
Links to authoritative sources (GvR, PEPs, docs, core devs notes) will be very appreciated.推荐答案
有关调用从协程协同例程
收益率()
感觉像一个函数调用(即,它会再次获得控制时,协程()完成)。For the calling co-routine
yield from coroutine()
feels like a function call (i.e. it will again gain control when coroutine() finishes).从任务(协程())
收益率,另一方面感觉更像是创建一个新线程。任务()
返回几乎立即机会是有,以前来电者获得控制权的协程()
结束。yield from Task(coroutine())
on the other hand feels more like creating a new thread.Task()
returns almost instantly and chances are there that the caller gains control beforecoroutine()
finishes.F的
的差异()
和TH = threading.Thread(目标= F,ARGS =()); th.start(); th.join()
是显而易见的,对吧?The difference between
f()
andth = threading.Thread(target=f, args=()); th.start(); th.join()
is obvious, right?这篇关于从协程VS产量从产量任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!