rust異步編程詳細(xì)講解
簡(jiǎn)化版本 Future
// wake 函數(shù) reactor發(fā)現(xiàn)狀態(tài)是ready 通知executor 函數(shù)
trait SimpleFuture {
type Output;
fn poll(&mut self, wake: fn()) -> Poll<Self::Output>;
// fn poll(&mut self, wake: u32) -> Poll<Self::Output>;
}
enum Poll<T> {
Ready(T),
Pending,
}
future 返回的是Poll枚舉 , 狀態(tài)有Ready ,pending 狀態(tài)
executor 調(diào)用 Future 任務(wù),Ready 執(zhí)行完成, pending 阻塞 執(zhí)行其他任務(wù)
reactor 檢查任務(wù)是否變成 ready
simpleFuture 是一個(gè)trait, 屬于struct MySleep
use std::thread;
use std::time::Duration;
trait SimpleFuture {
type Output;
// fn poll(&mut self, wake: fn()) -> Poll<Self::Output>;
fn poll(&mut self, wake: u32) -> Poll<Self::Output>;
}
enum Poll<T> {
Ready(T),
Pending,
}
// 自定義循環(huán)技術(shù)
struct MySleeper {
polls: u64,
wake: u32, // 簡(jiǎn)化使用整數(shù) 替換函數(shù)
}
impl MySleeper {
fn new(wake: u32) -> Self {
MySleeper {
polls: 0,
wake: wake,
}
}
}
static mut FINISHED: bool = false;
impl SimpleFuture for MySleeper {
type Output = ();
fn poll(&mut self, wake: u32) -> Poll<Self::Output> {
// 簡(jiǎn)化編程 使用unsafe 使用外部的變量
unsafe {
if FINISHED {
Poll::Ready(())
} else {
self.wake = wake;
self.polls += 1;
println!("polls = {}", self.polls);
Poll::Pending
}
}
}
}
// 定義自定義Reactor
struct MyReactor {
wake: u32,
// 通知exector
handle: Option<thread::JoinHandle<()>>, // 線程句柄
}
impl MyReactor {
fn new() -> Self {
MyReactor {
wake: 0,
handle: None,
}
}
// 知道哪個(gè)wake 通知具體函數(shù)
fn add_wake(&mut self, wake: u32) {
self.wake = wake;
}
// check status
fn check_status(&mut self) {
if self.handle.is_none() {
let wake = self.wake;
// 模擬 通過死循環(huán)進(jìn)行監(jiān)控狀態(tài)
let handle = thread::spawn(|| loop {
thread::sleep(Duration::from_secs(5));
unsafe {
//模擬future就緒,然后調(diào)用wake
FINISHED = true;
}
});
self.handle = Some(handle);
}
}
}
struct MyExecutor;
impl MyExecutor {
fn block_on<F: SimpleFuture>(mut myfuture: F, wake: u32) {
//阻塞執(zhí)行future
loop {
match myfuture.poll(wake) {
Poll::Ready(_) => {
println!("my future is ok");
break;
}
Poll::Pending => unsafe {
while !FINISHED {
// 循環(huán) 每隔一秒鐘檢測(cè)一下
thread::sleep(Duration::from_secs(1));
}
}
}
}
}
}
fn main() {
let mut reactor = MyReactor::new();
let sleeper = MySleeper::new(5);
let wake = sleeper.wake;
reactor.add_wake(wake);
reactor.check_status();
MyExecutor::block_on(sleeper, wake);
}在簡(jiǎn)化版本的Future 對(duì)象中 有定義MyReactor, 和 MyExecutor, MyReactor wake 函數(shù)進(jìn)行標(biāo)記后調(diào)用自定義的check_staus 模擬Future的就緒,調(diào)用wake 函數(shù)通知, 在MyExector 的block_on 函數(shù)中 通過wake函數(shù)匹配狀態(tài) 判讀任務(wù)是否已經(jīng)Ready
理解Future的模型
運(yùn)行時(shí)狀態(tài)的框架: async-std, futures 中已經(jīng)實(shí)現(xiàn)executor不需要自己在實(shí)現(xiàn)。
異步編程中,rust的編程語言中只給我們提供trait Future, async-std, tokio,futures 等異步編程庫 對(duì)其進(jìn)行擴(kuò)展,并且提供相對(duì)應(yīng)的函數(shù)
async fn hello() {
println!("hello");
}
等價(jià)于下面函數(shù)
fn hello() -> impl Future<Output=()> {
async {
println!("hello");
}
}rust 標(biāo)準(zhǔn)庫中 Future如下:
pub trait Future {
type Output;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>;
}
// Pin 可以創(chuàng)建不可移動(dòng)Future,通過不可移動(dòng)對(duì)象在他們字段中存儲(chǔ)指針
struct MyFut {
a: i32,
ptr: *const i32, // 指針指向字段a 所在的內(nèi)存首地址
}async/awit 使用
在閉包中使用async
use std::future::Future;
fn my_function() -> impl Future<Output = u8> {
let closure = async |x: u8| {
x
};
closure(5)
}
閉包在穩(wěn)定版本還不支持,后續(xù)的版本中會(huì)支持 異步閉包
##### async lifetime
```rust
這個(gè)函數(shù)的生命周期
// fn foo_expand(x: &'a u8) -> impl Future<Output = u8> + 'a {
// async {
// *x
// }
// }
async fn foo(x: & u8) -> u8 {
*x
}
// async 函數(shù)返回一個(gè)Future的對(duì)象
fn good() -> impl Future<Output = u8>{
// 異步代碼塊中,定義 變量后 調(diào)用foo_expand async 函數(shù)后進(jìn)行await
async {
// x 修飾后 x 的生命周期有多長(zhǎng) 就有多長(zhǎng)
let x = 5;
foo_expand(&x).await
}
}
到此這篇關(guān)于rust異步編程詳細(xì)講解的文章就介紹到這了,更多相關(guān)rust異步編程內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Rust中的Drop特性之解讀自動(dòng)化資源清理的魔法
Rust通過Drop特性實(shí)現(xiàn)了自動(dòng)清理機(jī)制,確保資源在對(duì)象超出作用域時(shí)自動(dòng)釋放,避免了手動(dòng)管理資源時(shí)可能出現(xiàn)的內(nèi)存泄漏或雙重釋放問題,智能指針如Box、Rc和RefCell都依賴于Drop來管理資源,提供了靈活且安全的資源管理方案2025-02-02
Rust?語言中符號(hào)?::?的使用場(chǎng)景解析
Rust?是一種強(qiáng)調(diào)安全性和速度的系統(tǒng)編程語言,這篇文章主要介紹了Rust?語言中符號(hào)?::?的使用場(chǎng)景,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-03-03
Rust調(diào)用函數(shù)操作符?.?和?::?的區(qū)別詳解
在Rust中,.和::操作符都可以用來調(diào)用方法,但它們的用法有所不同,所以本文就將詳細(xì)的給大家介紹一下.和::操作符的區(qū)別,感興趣的同學(xué)跟著小編一起來學(xué)習(xí)吧2023-07-07
Rust初體驗(yàn):手把手教你構(gòu)建‘Hello,?World!’
"準(zhǔn)備好了嗎?一起踏上Rust編程語言的精彩旅程!在這篇「Rust初體驗(yàn)」中,我們將手把手教你構(gòu)建經(jīng)典程序“Hello,?World!”,感受Rust的強(qiáng)大與安全,短短幾行代碼,就能讓你對(duì)這個(gè)系統(tǒng)級(jí)語言的魅力一探究竟!快加入吧,驚喜等你發(fā)現(xiàn)!"2024-01-01
詳解Rust調(diào)用tree-sitter支持自定義語言解析
使用Rust語言結(jié)合tree-sitter庫解析自定義語言需要定義語法、生成C解析器,并在Rust項(xiàng)目中集成,具體步驟包括創(chuàng)建grammar.js定義語法,使用tree-sitter-cli工具生成C解析器,以及在Rust項(xiàng)目中編寫代碼調(diào)用解析器,這一過程涉及到對(duì)tree-sitter的深入理解和Rust語言的應(yīng)用技巧2024-09-09
關(guān)于Rust?使用?dotenv?來設(shè)置環(huán)境變量的問題
在項(xiàng)目中,我們通常需要設(shè)置一些環(huán)境變量,用來保存一些憑證或其它數(shù)據(jù),這時(shí)我們可以使用dotenv這個(gè)crate,接下來通過本文給大家介紹Rust?使用dotenv來設(shè)置環(huán)境變量的問題,感興趣的朋友一起看看吧2022-01-01
Rust中類型轉(zhuǎn)換在錯(cuò)誤處理中的應(yīng)用小結(jié)
隨著項(xiàng)目的進(jìn)展,關(guān)于Rust的故事又翻開了新的一頁,今天來到了服務(wù)器端的開發(fā)場(chǎng)景,發(fā)現(xiàn)錯(cuò)誤處理中的錯(cuò)誤類型轉(zhuǎn)換有必要分享一下,對(duì)Rust錯(cuò)誤處理相關(guān)知識(shí)感興趣的朋友一起看看吧2023-09-09

