■ join! 매크로를 사용해 비동기 병렬 처리 완료시까지 대기하는 방법을 보여준다.
▶ 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 21 22 23 24 25 26 27 28 29 |
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; println!("------------"); tokio::join! ( print_message_async(2, "메시지 #4"), print_message_async(3, "메시지 #5"), print_message_async(1, "메시지 #6") ); } |