What's wrong with print! macro? #5746
-
I might be not understanding something, but take following code: use tokio::net::{TcpListener, TcpStream};
use tokio::time::{sleep, Duration};
use std::io;
#[tokio::main]
async fn main() -> io::Result<()> {
let listener = TcpListener::bind("127.0.0.1:8080").await?;
loop {
let (socket, address) = listener.accept().await?;
tokio::task::spawn(async move {
println!("new client: {:?}", &address);
sleep(Duration::from_secs(5)).await;
println!("test");
});
}
} launch it, and start several connections. You will see expected output, like this
everything correct, tasks processed concurrently. But now take the same code but with one change use tokio::net::{TcpListener, TcpStream};
use tokio::time::{sleep, Duration};
use std::io;
#[tokio::main]
async fn main() -> io::Result<()> {
let listener = TcpListener::bind("127.0.0.1:8080").await?;
loop {
let (socket, address) = listener.accept().await?;
tokio::task::spawn(async move {
println!("new client: {:?}", &address);
sleep(Duration::from_secs(5)).await;
print!("test"); // print! macro here
});
}
} and output is
like print! macro slept before accepting new connection or something like that. Is is valid behavior? |
Beta Was this translation helpful? Give feedback.
Answered by
Darksonn
May 31, 2023
Replies: 1 comment 1 reply
-
This is unrelated to Tokio. Generally, stdout is line buffered which means that the program doesn't actually send the bytes until you write a newline or flush stdout. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
Darksonn
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is unrelated to Tokio. Generally, stdout is line buffered which means that the program doesn't actually send the bytes until you write a newline or flush stdout.