Rust 是否需要另⼀种“⾊彩”的 Future? - 郭⼦兴
Rust 是否需要另⼀种“⾊彩”的 Future? 郭⼦兴,字节跳动服务框架团队研发⼯程师。 作者 Rust 是否需要另⼀种“⾊彩”的 Future? 标题 Rust 编译器将 async 块翻译成由标准库提供的 Future 类型,⽤户可以⽅便地通过⾃定义 Future 以实现⾮阻塞 的 IO 或并发控制语义。异步执⾏器被允许在任意时刻删除⽣成的 Future 实例以取消正在执⾏的异步操作,但取消 是否需要另⼀种“颜⾊”的 Future 为有副作⽤取消的异步⾏为提供安全保证? 回顾如何使⽤ Future ⽀持基于 poll 的 IO,以及基于取消 Future 的流程控制 # 基于 Poll 的 Future io-uring 等异步模型让 Future 的取消不 再⽆副作⽤,这可能导致严重的错误 # 不再是⽆副作⽤的取消 # 最终与折中⽅案 引⼊另⼀种“颜⾊”的 Future 以最终解决 Rust 中的异步 Rust 基于 Future trait 实现异步编程 pub trait Future { type Output; fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) �� Poll; } pub trait Future { type Output; 0 码力 | 19 页 | 7.77 MB | 1 年前3Rust 程序设计语言 简体中文版 1.85.0
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414 17.6. future、任务和线程 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . await 关键字。 future 是一个现在可能还没有准备好但将在未来某个时刻准备好的值。(相同的概念也出现在 很多语言中,有时被称为 “task” 或者 “promise”。)Rust 提供了 Future trait 作为基础组件, 这样不同的异步操作就可以在不同的数据结构上实现。在 Rust 中,我们称实现了 Future trait 的类型为 future。每个 future 会维护自身的进度状态信息以及对 并恢复。在一个 async 块或 async 函数中,可以使用 await 关键字来 await 一个 future(即等待其就绪)。async 块或 async 函 数中每一个等待 future 的地方都可能是一个 async 块或 async 函数中断并随后恢复的点。检 查一个 future 并查看其值是否已经准备就绪的过程被称为 轮询(polling)。 其它一些语言,例如 C# 和 JavaScript,也使用0 码力 | 562 页 | 3.23 MB | 9 天前3Comprehensive Rust(简体中文) 202412
#[deny(unsafe_op_in_unsafe_fn)]. Try adding it and see what happens. This will likely change in a future Rust edition. 30.6 实现 Unsafe Trait 与函数一样,如果您在实现某个 trait 时必须保证特定条件来避免未定义的行为,您也可以将该 trait 标 记为 unsafe。 Option、Result • Display、Debug、write!... • Iterator • panic!、assert_eq!... • NonNull 和所有常见的指针相关函数 • Future 和 async/await • fence、AtomicBool、AtomicPtr、AtomicU32... • Duration • Box、Cow、Arc、Rc • Vec、Binar Rust 的异步操作基于 ”Futures” 来实现,即表示未来可能完成的工作。系统会对这些 Future 进行“轮 询”,直到显示全部已完成。 由异步运行时对这些 Future 进行轮询,并且有多种不同的运行时可供选择。 比较 • Python 的 asyncio 中也有类似的模型。不过,其 Future 类型基于回调的实现方式,而非通过轮 询。使用异步 Python 程序需要类似于 Rust0 码力 | 359 页 | 1.33 MB | 10 月前3C++高性能并行编程与优化 - 课件 - 05 C++11 开始的多线程编程
std::async • std::async 接受一个带返回值的 lambda ,自身返回一个 std::future 对象 。 • lambda 的函数体将在另一个线程里执行 。 • 接下来你可以在 main 里面做一些别的事 情, download 会持续在后台悄悄运行。 • 最后调用 future 的 get() 方法,如果此时 download 还没完成,会等待 download 完成,并获取 则可以指定一个最长等待时间, 用 chrono 里的类表示单位。他会返回一个 std::future_status 表示等待是否成功。 • 如果超过这个时间线程还没有执行完毕,则放 弃等待,返回 future_status::timeout 。 • 如果线程在指定的时间内执行完毕,则认为等 待成功,返回 future_status::ready 。 • 同理还有 wait_until() 其参数是一个时间点。 d 做参数 • std::async 的第一个参数可以设为 std::launch::deferred ,这时不会创建一个 线程来执行,他只会把 lambda 函数体内 的运算推迟到 future 的 get() 被调用时。 也就是 main 中的 interact 计算完毕后。 • 这种写法, download 的执行仍在主线程 中,他只是函数式编程范式意义上的异步 ,而不涉及到真正的多线程。可以用这个0 码力 | 79 页 | 14.11 MB | 1 年前3Kotlin 1.9.10 官方文档 中文版
is to provide first-class support for the Android target. We're excited to announce that in the future, the Android team from Google will provide its own Gradle plugin to support Android in Kotlin Multiplatform Xcode integration tasks or the Kotlin CocoaPods Gradle plugin. We expect to add this feature in future Kotlin releases. Kotlin/Wasm The Kotlin team continues to experiment with the new Kotlin/Wasm target update your code to utilize the suggested external sealed class implementation for compatibility and future maintenance. 对 ES6 类与模块的实验性支持 This release introduces Experimental support for ES6 modules and0 码力 | 3753 页 | 29.69 MB | 1 年前3Await-Tree Async Rust 可观测性的灵丹妙药 - 赵梓淇
• 无栈协程 Async Rust 回顾 Rust 的无栈协程抽象 — Future Async Rust 回顾 • 通过 poll 驱动的状态机 • 组合嵌套为调度单元: Task • async fn 语法糖 Async Rust 观测与调试的痛点 Async Rust 回顾 • 特性: Future 灵活的可组合性 • 任意定制 Poll 的执行逻辑 (Join / Select 设计原理与实现 2 回顾 Async Rust 的设计与痛点 1 Await-Tree 的 应用与真实案例 3 设计目标 Await Tree 的设计原理与实现 • 追踪关键 Future 的生命周期和控制流 • Init, First Poll, Pending, Next Poll, Ready, Cancel • 实时将 Task 的执行状态维护为一棵树 • 显示目前正在阻塞 Tree 的设计原理与实现 设计细节 Await Tree 的设计原理与实现 • 充分理解 Future 生命周期中的控制流 Await Tree 的维护 Await Tree 的设计原理与实现 • 初始状态 Await Tree 的维护 Await Tree 的设计原理与实现 • Future 构造 Await Tree 的维护 Await Tree 的设计原理与实现 • Select0 码力 | 37 页 | 8.60 MB | 1 年前3Kotlin 官方文档中文版 v1.9
is to provide first-class support for the Android target. We're excited to announce that in the future, the Android team from Google will provide its own Gradle plugin to support Android in Kotlin Multiplatform Xcode integration tasks or the Kotlin CocoaPods Gradle plugin. We expect to add this feature in future Kotlin releases. Kotlin/Wasm The Kotlin team continues to experiment with the new Kotlin/Wasm update your code to utilize the suggested external sealed class implementation for compatibility and future maintenance. 对 ES6 类与模块的实验性支持 This release introduces Experimental support for ES6 modules and0 码力 | 2049 页 | 45.06 MB | 1 年前3Hyperledger Fabric 2.5 中文文档
的设置(或着说 环境变量 ,通常缩写为 env var )可以把退出换为警告,以 提供更灵活的错误处理。 让我们把这个设置丢到 test 函数中,然后注意这个 local 调用的结果: from __future__ import with_statement from fabric.api import local, settings, abort from fabric.contrib.console "): abort("Aborting at user request.") [...] 为了引入这个新特性,我们需要添加一些新东西: 在 Python 2.5 中,需要从 __future__ 中导入 with ; Fabric contrib.console 子模块提供了 confirm 函数,用于简单的 yes/no 提示。 settings 上下文管理器提供了特定代码块特殊设置的功能。 函数类似,只不过后者是在本地执行。 ~fabric.operations.run` 和 local 类似,不过是在 远程 而非本地执行。 我们还需要保证在文件顶部导入了这些新函数: from __future__ import with_statement from fabric.api import local, settings, abort, run, cd from fabric.contrib0 码力 | 138 页 | 154.00 KB | 1 年前3Hyperledger Fabric 1.4 中文文档
的设置(或着说 环境变量 ,通常缩写为 env var )可以把退出换为警告,以 提供更灵活的错误处理。 让我们把这个设置丢到 test 函数中,然后注意这个 local 调用的结果: from __future__ import with_statement from fabric.api import local, settings, abort from fabric.contrib.console "): abort("Aborting at user request.") [...] 为了引入这个新特性,我们需要添加一些新东西: 在 Python 2.5 中,需要从 __future__ 中导入 with ; Fabric contrib.console 子模块提供了 confirm 函数,用于简单的 yes/no 提示。 settings 上下文管理器提供了特定代码块特殊设置的功能。 函数类似,只不过后者是在本地执行。 ~fabric.operations.run` 和 local 类似,不过是在 远程 而非本地执行。 我们还需要保证在文件顶部导入了这些新函数: from __future__ import with_statement from fabric.api import local, settings, abort, run, cd from fabric.contrib0 码力 | 145 页 | 161.53 KB | 1 年前3现代C++ 教程:高速上手C++11/14/17/20
t1(critical_section, 2), t2(critical_section, 3); t1.join(); t2.join(); return 0; } 7.3 期物 期物(Future)表现为 std::future,它提供了一个访问异步操作结果的途径,这句话很不好理解。 为了理解这个特性,我们需要先理解一下在 C++11 之前的多线程行为。 试想,如果我们的主线程 A 希望新开辟一个线程 个特定的时间获得线程 B 的结果。 在 C++11 的 std::future 被引入之前,通常的做法是:创建一个线程 A,在线程 A 里启动任务 B,当准备完毕后发送一个事件,并将结果保存在全局变量中。而主函数线程 A 里正在做其他的事情,当 需要结果的时候,调用一个线程等待函数来获得执行的结果。 而 C++11 提供的 std::future 简化了这个流程,可以用来获取异步任务的结果。自然地,我们很 为了看一个例子,我们这里额外使用 std::packaged_task,它可以用来封装任何可以调用的目标, 从而用于实现异步的调用。举例来说: #include#include <future> #include int main() { // 将一个返回值为 7 的 lambda 表达式封装到 task 中 // std::packaged_task 的模板参数为要封装函数的类型 0 码力 | 83 页 | 2.42 MB | 1 年前3
共 221 条
- 1
- 2
- 3
- 4
- 5
- 6
- 23