#[tokio::main] asyncfnmain() ->Result<(), Box<dyn Error>> { // Parse the arguments, bind the TCP socket we'll be listening to, spin up // our worker threads, and start shipping sockets to those worker threads. letaddr = env::args() .nth(1) .unwrap_or_else(|| "127.0.0.1:8080".to_string()); letserver = TcpListener::bind(&addr).await?; println!("Listening on: {}", addr);
loop { let (stream, _) = server.accept().await?; tokio::spawn(asyncmove { letthread_id = std::thread::current().id(); println!("[UserFuc1] Thread ID in spawned thread: {:?}", thread_id);
ifletErr(e) = process(stream).await { println!("failed to process connection; error = {}", e); } letthread_id = std::thread::current().id(); println!("[UserFuc2] Thread ID in spawned thread: {:?}", thread_id);
test_fn().await; letthread_id = std::thread::current().id(); println!("[UserFuc3] Thread ID in spawned thread: {:?}", thread_id); }); } }
还好之前有阅读过 Rust 的异步编程文档, 其中就有提到 async transforms a block of code into a state machine that implements a trait called Future