zoukankan      html  css  js  c++  java
  • 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!
  • 相关阅读:
    数学思想方法-python计算战(8)-机器视觉-二值化
    04-05组合问题_算法训练
    至HDFS附加内容
    HDU 1010 Tempter of the Bone heuristic 修剪
    二叉树3种遍历的非递归算法
    [Ramda] R.project -- Select a Subset of Properties from a Collection of Objects in Ramda
    [Ramda] Refactor a Promise Chain to Function Composition using Ramda
    [SVG] Combine Multiple SVGs into an SVG Sprite
    [Ramda] Difference between R.converge and R.useWith
    [Ramda] Refactor to a Point Free Function with Ramda's useWith Function
  • 原文地址:https://www.cnblogs.com/dream397/p/14201957.html
Copyright © 2011-2022 走看看