惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Hackread – Cybersecurity News, Data Breaches, AI and More
W
WeLiveSecurity
C
Check Point Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Vulnerabilities – Threatpost
GbyAI
GbyAI
A
Arctic Wolf
NISL@THU
NISL@THU
N
Netflix TechBlog - Medium
The Register - Security
The Register - Security
M
MIT News - Artificial intelligence
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Microsoft Security Blog
Microsoft Security Blog
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
T
Tenable Blog
G
GRAHAM CLULEY
O
OpenAI News
S
Schneier on Security
Google Online Security Blog
Google Online Security Blog
Vercel News
Vercel News
宝玉的分享
宝玉的分享
Attack and Defense Labs
Attack and Defense Labs
T
The Blog of Author Tim Ferriss
量子位
aimingoo的专栏
aimingoo的专栏
The Cloudflare Blog
P
Privacy & Cybersecurity Law Blog
S
SegmentFault 最新的问题
MongoDB | Blog
MongoDB | Blog
Apple Machine Learning Research
Apple Machine Learning Research
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
LINUX DO - 热门话题
博客园_首页
F
Full Disclosure
Recent Commits to openclaw:main
Recent Commits to openclaw:main
D
Docker
U
Unit 42
A
About on SuperTechFans
博客园 - 司徒正美
Hacker News - Newest:
Hacker News - Newest: "LLM"
人人都是产品经理
人人都是产品经理
Application and Cybersecurity Blog
Application and Cybersecurity Blog
G
Google Developers Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
J
Java Code Geeks
云风的 BLOG
云风的 BLOG
Scott Helme
Scott Helme
TaoSecurity Blog
TaoSecurity Blog

hsfzxjy 的博客

解决 VSCode + CMake + MSVC 编译器信息乱码的问题 使用 3090 部署 1.58bit 动态量化版 DeepSeek R1 671b 如何在 VS Code DevContainer 中配置 HTTP 代理 如何在跳板机背后的服务器上使用 VS Code Remote - Containers Cohesive Digests for Ints and Floats Rust 中的隐匿概念 —— Place(位置) 美术馆 一尺之槌,日取其半,1075日而竭 老生常谈:使用 Cloudflare 自选 IP 加速站点访问 辩义 State、Nation 与 Country 将 Base64 编码的数据快速转换为 Uint8Array 折腾 NPU·第1章 —— 搭建 Level Zero 开发环境 折腾 NPU·第0章 —— Intel NPU 概述与 Level-Zero 新增域名 monad.run CSS 中为特定字符设置不同字体 Arbitary Lifetime Transmutation via Rust Unsoundness Dijkstra 算法的延伸 Manacher 回文计数算法 硬卧 Go Fact: Zero-sized Field at the Rear of a Struct Has Non-zero Size Display *big.Rat Losslessly and Smartly in Golang 代码的仪式 Building Electron From Scratch 中式亲属称谓研究之一:构建半群 Git sparse-checkout and partial clones for Mega-Repos 辩义“封建” Diving from the CUDA Error 804 into a bug of libnvidia-container Modern Cryptography, GPG and Integration with Git(hub) Move the Root Partition of Ubuntu A New Programmer Kicks a Roadblock Git-based Dependencies in Dart and Go Reversy Naming 人类一败涂地 Invalid Golang Pointers Can Bite You Even If You Don't Dereference Side Project(副业) A Flaw of Promoting Complex Trait Bounds in Rust Initialize Process Pool Worker with Individual Value Rust - Python FFI From Scratch [Extending Hexo For My Site] Part 1 [Extending Hexo For My Site] Part 0 Debug a 'torch.tensor(1).cuda()' hanging 不自由的互联网 Retrieve Contents over HTTP without curl or wget [Unravelling mocona] Part 1 - Verbosity or Anti-Pattern [Unravelling mocona] Part 0 - Preface Understanding pickle in Python Rough Notes on Deploying Vaultwarden & NextCloud Bookmarks 语言狂热者与实用主义者 Demystify the randomness in CUDA kernels Performant Bulk Mutations in IndexedDB Auto Rebuild .pyx Files with pyximport Cython and Threads Obtain a Random Available TCP Port with Bash Information Theory: KL Divergence Information Theory: Entropy and Mutual Information 铁板烧 西郊线 Proof of the Gumbel Max Trick Option::as_ref Rc, RefCell and Interior Mutability Visualizing Correlation 三月十日杂感 三月一日杂感 二月十一日杂感 一月二十六日杂感 SS Configuration 一月七日杂感 四月·病 Haskell 笔记:State Monad Haskell 笔记:Monad 引论 Haskell 笔记:Applicative Haskell 笔记:Category Theory and Functor Haskell 笔记:data, type, newtype Haskell 笔记:folds 使用 Aria2 在 Ubuntu 中下载百度云资源 从伪并行的 Python 多线程说起 一个 Reentrant Error 引发的对 Python 信号机制的探索和思考 Linux 文件权限 HSFZMUN 4.0 部署小记 午后雨·科大 最后的雨夜·广州 揭秘·变态的平方根倒数算法 神坑·Python 装饰类无限递归 Python“黑魔法”之 Encoding & Decoding Ubuntu 重新映射键盘布局 为什么我要翻墙 Python“黑魔法”之 Generator Coroutines 数学美 之 判断线段相交的最简方法 除夕杂感 17 行代码实现的简易 Javascript 字符串模板 Python“黑魔法”之 Meta Classes 诗集 生活,需要被“发现” 家书·十八岁成人礼 炫技?还是需求? 【译】响应式图片的现状 【译】“为什么有这么多的编程语言?” Wisecity 商赛总结——也谈前端自动化测试 记一次 DoS 诈骗网站的经历 那一年,我们望向星空
Some Notes on Kotlin Coroutines
2023-05-10 · via hsfzxjy 的博客

This post is written to dictate some opinionated explanation that dispels my confusion to Kotlin coroutines during learning.

suspend keyword colors a function

The suspend keyword in Kotlin colors a function, just like the async keyword in Javascript and Python, to inform the compiler that it might execute some asynchronous tasks.

suspending functions, such as the delay() function, must only be invoked within another suspending function.

suspend fun foo() {
delay(1000)
}
fun bar() {
delay(1000)
}
async def foo():
await asyncio.sleep(1)

def bar():
await asyncio.sleep(1)

Invocation of suspending functions are sequential by default

If two suspending functions fun1() and fun2() are invoked one followed with another, their invocations are sequentialized, i.e., only after fun1() finished will fun2() be started. This is in contrast with Javascript or Python, where async-functions must be explicitly await-ed to sequentialize.

suspend fun fun1() { delay(1000) }
suspend fun fun2() { delay(2000) }
suspend fun foo() {
fun1()
fun2()

}
async def fun1(): await asyncio.sleep(1)
async def fun2(): await asyncio.sleep(2)
async def foo():
fun1()
fun2()

In both of the preceding codes, fun1() and fun2() are invoked without additional syntax constructs. In Kotlin the invocations are sequential by default, and thus foo() will return in 3 seconds. In Python, the coroutines created are leaked without any execution, causing foo() to return immediately, and hopefully you will get a RuntimeWarning: coroutine 'fun1' was never awaited as alert. To equalize them, the Python version should use explicit await:

suspend fun fun1() { delay(1000) }
suspend fun fun2() { delay(2000) }
suspend fun foo() {
fun1()
fun2()

}
async def fun1(): await asyncio.sleep(1)
async def fun2(): await asyncio.sleep(2)
async def foo():
await fun1()
await fun2()

Such difference between Kotlin and other languages prevents the case of developers forgetting to write await and leaking coroutines, which is a foundation of ergonomic structural concurrency.

suspend function scope != coroutine scope

In Kotlin, coroutine builders such as launch() or async() must be invoked within a coroutine scope, since they are effectively extension methods of class CoroutineScope. However, a suspend function does not essentially form a coroutine scope, which means the following code is invalid:

suspend fun foo() {
launch { delay(1000) }
}

Instead, one should wrap the function with a coroutineScope call to make use of the extensions:

suspend fun foo() {
suspend fun foo() = coroutineScope {
launch { delay(1000) }
}
suspend fun foo() {
launch { delay(1000) }
}
suspend fun foo() = coroutineScope {
launch { delay(1000) }
}

Apart from coroutineScope(), other functions like launch(), async() or runBlocking() also create coroutine scopes.

Coroutine scope is the effective way to structural concurrency

Coroutine scopes are hierarchical. When calling .launch() or .async() from a outer scope, a child scope is inherited and created from it, implicitly forming a tree-like invocation structure.

fun main(): Unit = runBlocking(CoroutineName("A")) {
launch(CoroutineName("B")) {
launch(CoroutineName("C")) {
delay(1000)
println(2)
}
println(1)
}.join()
launch(CoroutineName("D")) { println(3) }
}

In the above program, we explictly attributes the name of different coroutines in order to refer them crystally. The program will create a conceptual hierarchy as below

Such hierarchy constrains the lifecycle of coroutines and therefore derives the structural concurrency. Specifically, we have –

Coroutine scopes are self-contained. A parental coroutine always waits for completion of all its children. Cancelling a parental coroutine recursively cancels all its children. The rules implies the program above would output like:

Since launch(CoroutineName("B")) {...}.join() would block until its child coroutine-C returns in one second. The self-containing property enables ergonomic cooperative job cancellation and ensures coroutines won’t be readily leaked 1.


Author: hsfzxjy.
Link: .
License: CC BY-NC-ND 4.0.
All rights reserved by the author.
Commercial use of this post in any form is NOT permitted.
Non-commercial use of this post should be attributed with this block of text.