From 0ec157244df6ddd255570a07e2427567029a8ce1 Mon Sep 17 00:00:00 2001 From: H2Sxxa Date: Thu, 1 Aug 2024 02:08:27 +0000 Subject: [PATCH] deploy: 57496a91e691d5b47bf8aa0c60c814fc7d28fee4 --- 2022/10/16/mcforge1.12.2-0/index.html | 2 +- 2022/10/16/mcforge1.12.2-1/index.html | 2 +- 2022/11/06/aimp_stereo_install/index.html | 2 +- 2023/03/18/maa_wifi_connect/index.html | 2 +- 2023/07/11/rosa_dev_exp/index.html | 8 ++++---- 2023/07/16/forge_dev_gradlemirror/index.html | 2 +- 2023/08/10/wsl_c_dev_vscode/index.html | 2 +- 2023/09/17/about_code_aesthetic/index.html | 2 +- 2023/09/17/about_oo_and_po/index.html | 2 +- 2024/02/10/how_to_use_rinf/index.html | 2 +- 2024/05/08/oop_with_fp_via_method_chain/index.html | 2 +- 2024/07/25/async_block_concurrent/index.html | 6 +++--- about/index.html | 6 +++--- board/index.html | 4 ++-- index.html | 4 ++-- local-search.xml | 6 +++--- 16 files changed, 27 insertions(+), 27 deletions(-) diff --git a/2022/10/16/mcforge1.12.2-0/index.html b/2022/10/16/mcforge1.12.2-0/index.html index a49b4b1..550273c 100644 --- a/2022/10/16/mcforge1.12.2-0/index.html +++ b/2022/10/16/mcforge1.12.2-0/index.html @@ -25,7 +25,7 @@ - + diff --git a/2022/10/16/mcforge1.12.2-1/index.html b/2022/10/16/mcforge1.12.2-1/index.html index 04d00cd..790cbcf 100644 --- a/2022/10/16/mcforge1.12.2-1/index.html +++ b/2022/10/16/mcforge1.12.2-1/index.html @@ -25,7 +25,7 @@ - + diff --git a/2022/11/06/aimp_stereo_install/index.html b/2022/11/06/aimp_stereo_install/index.html index 877a760..029fbc4 100644 --- a/2022/11/06/aimp_stereo_install/index.html +++ b/2022/11/06/aimp_stereo_install/index.html @@ -25,7 +25,7 @@ - + diff --git a/2023/03/18/maa_wifi_connect/index.html b/2023/03/18/maa_wifi_connect/index.html index c7cc974..43aea79 100644 --- a/2023/03/18/maa_wifi_connect/index.html +++ b/2023/03/18/maa_wifi_connect/index.html @@ -25,7 +25,7 @@ - + diff --git a/2023/07/11/rosa_dev_exp/index.html b/2023/07/11/rosa_dev_exp/index.html index 539780a..afec0cc 100644 --- a/2023/07/11/rosa_dev_exp/index.html +++ b/2023/07/11/rosa_dev_exp/index.html @@ -25,11 +25,11 @@ - + - + @@ -354,10 +354,10 @@

总结#Dart - #Gradle - #Flutter + #Gradle + diff --git a/2023/07/16/forge_dev_gradlemirror/index.html b/2023/07/16/forge_dev_gradlemirror/index.html index cfaef04..172057f 100644 --- a/2023/07/16/forge_dev_gradlemirror/index.html +++ b/2023/07/16/forge_dev_gradlemirror/index.html @@ -25,7 +25,7 @@ - + diff --git a/2023/08/10/wsl_c_dev_vscode/index.html b/2023/08/10/wsl_c_dev_vscode/index.html index ecf1fef..ee95216 100644 --- a/2023/08/10/wsl_c_dev_vscode/index.html +++ b/2023/08/10/wsl_c_dev_vscode/index.html @@ -25,7 +25,7 @@ - + diff --git a/2023/09/17/about_code_aesthetic/index.html b/2023/09/17/about_code_aesthetic/index.html index f4f106f..d211f34 100644 --- a/2023/09/17/about_code_aesthetic/index.html +++ b/2023/09/17/about_code_aesthetic/index.html @@ -25,7 +25,7 @@ - + diff --git a/2023/09/17/about_oo_and_po/index.html b/2023/09/17/about_oo_and_po/index.html index a97a305..a0e10af 100644 --- a/2023/09/17/about_oo_and_po/index.html +++ b/2023/09/17/about_oo_and_po/index.html @@ -25,7 +25,7 @@ - + diff --git a/2024/02/10/how_to_use_rinf/index.html b/2024/02/10/how_to_use_rinf/index.html index 0df7953..bb2a479 100644 --- a/2024/02/10/how_to_use_rinf/index.html +++ b/2024/02/10/how_to_use_rinf/index.html @@ -25,7 +25,7 @@ - + diff --git a/2024/05/08/oop_with_fp_via_method_chain/index.html b/2024/05/08/oop_with_fp_via_method_chain/index.html index 5960e01..d22c881 100644 --- a/2024/05/08/oop_with_fp_via_method_chain/index.html +++ b/2024/05/08/oop_with_fp_via_method_chain/index.html @@ -25,7 +25,7 @@ - + diff --git a/2024/07/25/async_block_concurrent/index.html b/2024/07/25/async_block_concurrent/index.html index 1860c1d..366856f 100644 --- a/2024/07/25/async_block_concurrent/index.html +++ b/2024/07/25/async_block_concurrent/index.html @@ -25,7 +25,7 @@ - + @@ -250,7 +250,7 @@ @@ -320,7 +320,7 @@

https://docs.rs/tokio/latest/tokio/macro.join.html#runtime-characteristics

-

join 具有并发性, spawn 还具有并行性,对于I/O密集型任务,有一段等待返回数据的时间,join/spawn性能差异不大,这也是异步在I/O密集型场景高效的原因。

+

join 具有并发性, spawn 还具有并行性,对于I/O密集型任务,有一段等待数据的时间,join/spawn性能差异不大,这也是异步在I/O密集型场景高效的原因。

CPU密集型任务请使用 spawn

在 Dart 中进行并发

著名的单线程语言是 JavaScript,很遗憾,笔者没有接触过这门以函数出名的语言。

不过除了 JavaScript 以外,因 Flutter 而出名的 Dart 也是一门优秀的单线程语言 (尽管 Isolate 令人印象深刻)。

diff --git a/about/index.html b/about/index.html index 1ee28ee..1301d58 100644 --- a/about/index.html +++ b/about/index.html @@ -25,8 +25,8 @@ - - + + @@ -305,7 +305,7 @@

Contact 📞

diff --git a/board/index.html b/board/index.html index 2a45a38..d56036b 100644 --- a/board/index.html +++ b/board/index.html @@ -23,8 +23,8 @@ - - + + diff --git a/index.html b/index.html index 7de839f..6ca4f42 100644 --- a/index.html +++ b/index.html @@ -814,10 +814,10 @@

#Dart - #Gradle - #Flutter + #Gradle + diff --git a/local-search.xml b/local-search.xml index 785151b..853e517 100644 --- a/local-search.xml +++ b/local-search.xml @@ -8,7 +8,7 @@ /2024/07/25/async_block_concurrent/ - 阻塞

引言: 什么是 await ?

在异步编程中,await 是一个很重要的语法,使用 await 来解析异步操作等待获取值是很常见的事情,因此,初学异步编程时,我们会理所当然地在所有地方使用 await

但你真的确定你知道 await 为你做了什么吗?以及你是否有思考过,这样使用 await 最终和同步代码相比究竟有什么不同?

本篇文章就以这两个问题出发,对异步编程进行一些思考,希望对你学习异步编程能有帮助。

如有错误,请不吝赐教!

await 与阻塞

毫无疑问 await 是阻塞的,你可以尝试运行以下的代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
#[tokio::test]
async fn await_block() {
use std::time::Duration;

async fn foo(count: u64) {
tokio::time::sleep(Duration::from_secs(count)).await;
println!("{}", count);
}

foo(3).await;
foo(2).await;
foo(1).await;
}

输出结果是 3 2 1

很显然,这一段代码与它的同步的差别不大。

对于一些 GUI程序 来说,阻塞是致命的,阻塞意味着停止渲染 UI,这将会导致程序界面的卡死,尽管实际上程序正在运行。

并发与并行

多线程语言的 join/spawn

1
2
3
4
5
6
7
8
9
10
11
#[tokio::test]
async fn await_block() {
use std::time::Duration;

async fn foo(count: u64) {
tokio::time::sleep(Duration::from_secs(count)).await;
println!("{}", count);
}

tokio::join!(foo(3), foo(2), foo(1)); // tokio::spawn(foo(3)); ...
}

输出结果将是 1 2 3 意味着你可以拿它来写睡眠排序

By running all async expressions on the current task, the expressions are able to run concurrently but not in parallel. This means all expressions are run on the same thread and if one branch blocks the thread, all other expressions will be unable to continue. If parallelism is required, spawn each async expression using tokio::spawn and pass the join handle to join!.

https://docs.rs/tokio/latest/tokio/macro.join.html#runtime-characteristics

join 具有并发性, spawn 还具有并行性,对于I/O密集型任务,有一段等待返回数据的时间,join/spawn性能差异不大,这也是异步在I/O密集型场景高效的原因。

CPU密集型任务请使用 spawn

在 Dart 中进行并发

著名的单线程语言是 JavaScript,很遗憾,笔者没有接触过这门以函数出名的语言。

不过除了 JavaScript 以外,因 Flutter 而出名的 Dart 也是一门优秀的单线程语言 (尽管 Isolate 令人印象深刻)。

下面这个例子会输出 1 2 3

如果在所有调用 foo 的地方加上 await,那就会变成 3 2 1

1
2
3
4
5
6
7
8
9
10
void main() async {
foo(int sec) async =>
Future.delayed(Duration(seconds: sec)).then((_) => print(sec));

foo(3);
foo(2);
foo(1);

await Future.delayed(const Duration(seconds: 6));
}

如果你关注异步任务的返回值,你可以使用 Future.wait.

1
2
3
4
5
6
void main() async {
foo(int sec) async =>
Future.delayed(Duration(seconds: sec)).then((_) => print(sec));

var data = await Future.wait(List.generate(3, (index) => Future(()async=> foo(index))));
}

参考

下面这两篇文章写的非常清晰,推荐阅读

https://bingowith.me/2021/05/09/translation-async-what-is-blocking/

https://medium.com/flutter-community/futures-async-await-threading-in-flutter-baeeab1c1fe3

]]> + 阻塞

引言: 什么是 await ?

在异步编程中,await 是一个很重要的语法,使用 await 来解析异步操作等待获取值是很常见的事情,因此,初学异步编程时,我们会理所当然地在所有地方使用 await

但你真的确定你知道 await 为你做了什么吗?以及你是否有思考过,这样使用 await 最终和同步代码相比究竟有什么不同?

本篇文章就以这两个问题出发,对异步编程进行一些思考,希望对你学习异步编程能有帮助。

如有错误,请不吝赐教!

await 与阻塞

毫无疑问 await 是阻塞的,你可以尝试运行以下的代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
#[tokio::test]
async fn await_block() {
use std::time::Duration;

async fn foo(count: u64) {
tokio::time::sleep(Duration::from_secs(count)).await;
println!("{}", count);
}

foo(3).await;
foo(2).await;
foo(1).await;
}

输出结果是 3 2 1

很显然,这一段代码与它的同步的差别不大。

对于一些 GUI程序 来说,阻塞是致命的,阻塞意味着停止渲染 UI,这将会导致程序界面的卡死,尽管实际上程序正在运行。

并发与并行

多线程语言的 join/spawn

1
2
3
4
5
6
7
8
9
10
11
#[tokio::test]
async fn await_block() {
use std::time::Duration;

async fn foo(count: u64) {
tokio::time::sleep(Duration::from_secs(count)).await;
println!("{}", count);
}

tokio::join!(foo(3), foo(2), foo(1)); // tokio::spawn(foo(3)); ...
}

输出结果将是 1 2 3 意味着你可以拿它来写睡眠排序

By running all async expressions on the current task, the expressions are able to run concurrently but not in parallel. This means all expressions are run on the same thread and if one branch blocks the thread, all other expressions will be unable to continue. If parallelism is required, spawn each async expression using tokio::spawn and pass the join handle to join!.

https://docs.rs/tokio/latest/tokio/macro.join.html#runtime-characteristics

join 具有并发性, spawn 还具有并行性,对于I/O密集型任务,有一段等待数据的时间,join/spawn性能差异不大,这也是异步在I/O密集型场景高效的原因。

CPU密集型任务请使用 spawn

在 Dart 中进行并发

著名的单线程语言是 JavaScript,很遗憾,笔者没有接触过这门以函数出名的语言。

不过除了 JavaScript 以外,因 Flutter 而出名的 Dart 也是一门优秀的单线程语言 (尽管 Isolate 令人印象深刻)。

下面这个例子会输出 1 2 3

如果在所有调用 foo 的地方加上 await,那就会变成 3 2 1

1
2
3
4
5
6
7
8
9
10
void main() async {
foo(int sec) async =>
Future.delayed(Duration(seconds: sec)).then((_) => print(sec));

foo(3);
foo(2);
foo(1);

await Future.delayed(const Duration(seconds: 6));
}

如果你关注异步任务的返回值,你可以使用 Future.wait.

1
2
3
4
5
6
void main() async {
foo(int sec) async =>
Future.delayed(Duration(seconds: sec)).then((_) => print(sec));

var data = await Future.wait(List.generate(3, (index) => Future(()async=> foo(index))));
}

参考

下面这两篇文章写的非常清晰,推荐阅读

https://bingowith.me/2021/05/09/translation-async-what-is-blocking/

https://medium.com/flutter-community/futures-async-await-threading-in-flutter-baeeab1c1fe3

]]>
@@ -239,10 +239,10 @@ Dart - Gradle - Flutter + Gradle +