■ spawn 함수를 사용해 비동기 병렬 처리하는 방법을 보여준다.
▶ Cargo.toml
1 2 3 4 5 6 7 8 9 |
[package] name = "test_project" version = "0.1.0" edition = "2021" [dependencies] tokio = { version = "1", features = ["full"] } |
▶ src/main.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
use tokio::time; async fn print_message_async(second_count : u64, message : &str) { time::sleep(time::Duration::from_secs(second_count)).await; println!("{} : {}", second_count, message); } #[tokio::main] async fn main() { tokio::spawn(print_message_async(3, "메시지 #1")); tokio::spawn(print_message_async(2, "메시지 #2")); tokio::spawn(print_message_async(1, "메시지 #3")); time::sleep(time::Duration::from_secs(4)).await; } |