Add two bins using crate delay_timer.
This commit is contained in:
		
							parent
							
								
									134b34a65f
								
							
						
					
					
						commit
						82ec2c819b
					
				
					 2 changed files with 93 additions and 0 deletions
				
			
		
							
								
								
									
										48
									
								
								src/bin/using-crate-delay_timer-in-async-context.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								src/bin/using-crate-delay_timer-in-async-context.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,48 @@
 | 
			
		|||
use delay_timer::prelude::*;
 | 
			
		||||
 | 
			
		||||
use anyhow::Result;
 | 
			
		||||
 | 
			
		||||
use smol::Timer;
 | 
			
		||||
use std::time::Duration;
 | 
			
		||||
 | 
			
		||||
#[tokio::main]
 | 
			
		||||
async fn main() -> Result<()> {
 | 
			
		||||
    // In addition to the mixed (smol & tokio) runtime
 | 
			
		||||
    // You can also share a tokio runtime with delayTimer, please see api `DelayTimerBuilder::tokio_runtime` for details.
 | 
			
		||||
 | 
			
		||||
    // Build an DelayTimer that uses the default configuration of the Smol runtime internally.
 | 
			
		||||
    let delay_timer = DelayTimerBuilder::default().build();
 | 
			
		||||
 | 
			
		||||
    // Develop a print job that runs in an asynchronous cycle.
 | 
			
		||||
    let task_instance_chain = delay_timer.insert_task(build_task_async_print()?)?;
 | 
			
		||||
 | 
			
		||||
    // Get the running instance of task 1.
 | 
			
		||||
    let task_instance = task_instance_chain.next_with_async_wait().await?;
 | 
			
		||||
 | 
			
		||||
    // Cancel running task instances.
 | 
			
		||||
    task_instance.cancel_with_async_wait().await?;
 | 
			
		||||
 | 
			
		||||
    // Remove task which id is 1.
 | 
			
		||||
    delay_timer.remove_task(1)?;
 | 
			
		||||
 | 
			
		||||
    // No new tasks are accepted; running tasks are not affected.
 | 
			
		||||
    Ok(delay_timer.stop_delay_timer()?)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn build_task_async_print() -> Result<Task, TaskError> {
 | 
			
		||||
    let mut task_builder = TaskBuilder::default();
 | 
			
		||||
 | 
			
		||||
    let body = || async {
 | 
			
		||||
        println!("create_async_fn_body!");
 | 
			
		||||
 | 
			
		||||
        Timer::after(Duration::from_secs(3)).await;
 | 
			
		||||
 | 
			
		||||
        println!("create_async_fn_body:i'success");
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    task_builder
 | 
			
		||||
        .set_task_id(1)
 | 
			
		||||
        .set_frequency_repeated_by_seconds(6)
 | 
			
		||||
        .set_maximum_parallel_runnable_num(2)
 | 
			
		||||
        .spawn_async_routine(body)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										45
									
								
								src/bin/using-crate-delay_timer-internal.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								src/bin/using-crate-delay_timer-internal.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,45 @@
 | 
			
		|||
use anyhow::Result;
 | 
			
		||||
use delay_timer::prelude::*;
 | 
			
		||||
use smol::Timer;
 | 
			
		||||
use std::time::Duration;
 | 
			
		||||
 | 
			
		||||
fn main() -> Result<()> {
 | 
			
		||||
    // Build an DelayTimer that uses the default configuration of the `smol` runtime internally.
 | 
			
		||||
    let delay_timer = DelayTimerBuilder::default().build();
 | 
			
		||||
 | 
			
		||||
    // Develop a print job that runs in an asynchronous cycle.
 | 
			
		||||
    // A chain of task instances.
 | 
			
		||||
    let task_instance_chain = delay_timer.insert_task(build_task_async_print()?)?;
 | 
			
		||||
 | 
			
		||||
    // Get the running instance of task 1.
 | 
			
		||||
    let task_instance = task_instance_chain.next_with_wait()?;
 | 
			
		||||
 | 
			
		||||
    // Cancel running task instances.
 | 
			
		||||
    task_instance.cancel_with_wait()?;
 | 
			
		||||
 | 
			
		||||
    // Remove task which id is 1.
 | 
			
		||||
    delay_timer.remove_task(1)?;
 | 
			
		||||
 | 
			
		||||
    // No new tasks are accepted; running tasks are not affected.
 | 
			
		||||
    delay_timer.stop_delay_timer()?;
 | 
			
		||||
 | 
			
		||||
    Ok(())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn build_task_async_print() -> Result<Task, TaskError> {
 | 
			
		||||
    let mut task_builder = TaskBuilder::default();
 | 
			
		||||
 | 
			
		||||
    let body = || async {
 | 
			
		||||
        println!("create_async_fn_body!");
 | 
			
		||||
 | 
			
		||||
        Timer::after(Duration::from_secs(3)).await;
 | 
			
		||||
 | 
			
		||||
        println!("create_async_fn_body:i'success");
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    task_builder
 | 
			
		||||
        .set_task_id(1)
 | 
			
		||||
        .set_frequency_repeated_by_seconds(1)
 | 
			
		||||
        .set_maximum_parallel_runnable_num(2)
 | 
			
		||||
        .spawn_async_routine(body)
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		
		Reference in a new issue