欢迎光临散文网 会员登陆 & 注册

【Rust】 Tokio异步运行时 官方教程中的部分Topic 及select!宏用法

2023-03-26 05:02 作者:Louis翔  | 我要投稿


同步和异步的桥梁

spawn_blocking

spawn_blocking

异步地执行一个同步的代码块或者函数

不同于tokio::spawn(async {...});spawn_blocking会等待直到传入的同步函数完成,spawn会在主函数结束后中断

#[tokio::main]

These two are the same

Create a runtime and call block_on

执行在Runtime上的block_on()方法,从而像同步一样执行异步代码

对于以上方法,可以利用block_on轻易用于同步

Create a runtime and spawn things on it

Run the runtime in a separate thread and send message to it

Graceful Shutdown

figuring out when to shut down

Telling every part of the program to shut down


Waiting for other parts of the program to shut down

Select!宏 例子



【Rust】 Tokio异步运行时 官方教程中的部分Topic 及select!宏用法的评论 (共 条)

分享到微博请遵守国家法律