rust async

use futures::{ self, executor};
use std::thread;
use std::time;
async fn song() {
                loop {

    println!("song!");
        thread::sleep(time::Duration::from_secs(5));
                }
}

async fn dance() {
                loop {
        thread::sleep(time::Duration::from_secs(5));
    println!("Dance!");
                }
}

async fn async_main() {
    let f1 = song();
    let f2 = dance();
    futures::join!(f1, f2);
}
fn main() {
    executor::block_on(async_main());
    println!("Hello, world!");
}

没有dance

[root@bogon async4]# cargo build
   Compiling own v0.1.0 (/data2/rust/async4)
    Finished dev [unoptimized + debuginfo] target(s) in 0.58s
[root@bogon async4]# cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.03s
     Running `target/debug/own`
song!
song!
song!
song!
song!
song!
song!
song!
song!
^C
thread::sleep改成
sleep(time::Duration::from_secs(5)).await;
//例子二
use futures::{ self, executor};
use std::thread;
use std::time;
use async_std::task::{sleep, spawn};
async fn song() {
                loop {

    println!("song!");
        sleep(time::Duration::from_secs(5)).await;
                }
}

async fn dance() {
                loop {
        sleep(time::Duration::from_secs(5)).await;
    println!("Dance!");
                }
}

async fn async_main() {
    let f1 = song();
    let f2 = dance();
    futures::join!(f1, f2);
}
fn main() {
    executor::block_on(async_main());
    println!("Hello, world!");
}
use futures::{ self, executor};
use std::thread;
use std::time;
use async_std::task::{sleep, spawn};

async fn async_main() {
                loop {
        sleep(time::Duration::from_secs(5)).await;
    println!("Dance!");
                }
}
fn main() {
    executor::block_on(async_main());
    println!("Hello, world!");
}
warning: 2 warnings emitted

    Finished dev [unoptimized + debuginfo] target(s) in 0.06s
     Running `target/debug/own`
Dance!
Dance!
^C
use futures::{ self, executor};
use std::thread;
use std::time;
use async_std::task::{sleep, spawn};

async fn async_main() {
                loop {
        //sleep(time::Duration::from_secs(5)).await;
        sleep(time::Duration::from_secs(5));  //sleep不会执行
    println!("Dance!");
                }
}
fn main() {
    executor::block_on(async_main());
    println!("Hello, world!");
}
use futures::{ self, executor,join};
use std::thread;
use std::time;
use async_std::task::{sleep};

async fn async_main() {
                loop {
        //sleep(time::Duration::from_secs(5)).await;
        let f1 = sleep(time::Duration::from_secs(5));
        join!(f1);
    println!("Dance!");
                }
}
fn main() {
    executor::block_on(async_main());
    println!("Hello, world!");
}

顺序执行 --串联

use futures::{ self, executor,join};
use std::thread;
use std::time;
use async_std::task::{sleep};
async fn worker1(){
    println!("start worker1!");
        sleep(time::Duration::from_secs(5)).await;
    println!("stop worker1!");
}
async fn worker2(){
    println!("start worker2!");
        sleep(time::Duration::from_secs(5)).await;
    println!("stop worker2!");
}
async fn worker3(){
    println!("start worker3!");
        sleep(time::Duration::from_secs(5)).await;
    println!("stop worker3!");
}
async fn async_main() {
    worker1().await;
    worker2().await;
    worker3().await;
}
fn main() {
    executor::block_on(async_main());
    println!("Hello, world!");
}
Finished dev [unoptimized + debuginfo] target(s) in 0.06s
     Running `target/debug/own`
start worker1!
stop worker1!
start worker2!
stop worker2!
start worker3!
stop worker3!
Hello, world!

并联

//例子二
use futures::{ self, executor,join};
use std::thread;
use std::time;
use async_std::task::{sleep};
async fn worker1(){
    println!("start worker1!");
        sleep(time::Duration::from_secs(5)).await;
    println!("stop worker1!");
}
async fn worker2(){
    println!("start worker2!");
        sleep(time::Duration::from_secs(5)).await;
    println!("stop worker2!");
}
async fn worker3(){
    println!("start worker3!");
        sleep(time::Duration::from_secs(5)).await;
    println!("stop worker3!");
}
async fn async_main() {
        // sequence
    //worker1().await;
    //worker2().await;
    //worker3().await;
        //concurrence
        join!( worker1(), worker2(), worker3());
}
fn main() {
    executor::block_on(async_main());
    println!("Hello, world!");
}
Finished dev [unoptimized + debuginfo] target(s) in 0.06s
     Running `target/debug/own`
start worker1!
start worker2!
start worker3!
stop worker1!
stop worker2!
stop worker3!
Hello, world!

串联 + 并联

use futures::{ self, executor,join};
use std::thread;
use std::time;
use async_std::task::{sleep};
async fn worker1(){
    println!("start worker1!");
        sleep(time::Duration::from_secs(5)).await;
    println!("stop worker1!");
}
async fn worker2(){
    println!("start worker2!");
        sleep(time::Duration::from_secs(5)).await;
    println!("stop worker2!");
}
async fn worker3(){
    println!("start worker3!");
        sleep(time::Duration::from_secs(5)).await;
    println!("stop worker3!");
}
async fn async_main() {
        // sequence
    //worker1().await;
    //worker2().await;
    //worker3().await;
        //concurrence
        //join!( worker1(), worker2(), worker3());
        let f1 = async {
    worker1().await;
    worker2().await;
        };
        join!( f1, worker3());

}
fn main() {
    executor::block_on(async_main());
    println!("Hello, world!");
}
Finished dev [unoptimized + debuginfo] target(s) in 0.06s
     Running `target/debug/own`
start worker1!
start worker3!
stop worker1!
start worker2!
stop worker3!
stop worker2!
Hello, world!
(0)

相关推荐