i am trying to call a function which firsts sets up a TCPListener, then passes this to a function to poll the incoming tcp streams and then call a handle function on these streams, i can get it working with rayon but as my use case will have blocking I/O better to use tokio, when send test tcp connection request ive determined it never calls the handle_tcp_client function on first request (put println!("test") at top and never printed) but then after that it does get called (prints it) and returns expected to requesting tcp client
//in main (returns true):
match start_tcp_server(20000){
true => (),
false => return
}
//last line of start_tcp_server:
start_thread(run_tcp_server(socket.into()))
//run tcp server function
async fn run_tcp_server(listener: TcpListener){
for stream in listener.incoming() {
match stream{
Ok(new_stream) => _ = start_thread(handle_tcp_client(new_stream)),
// tests show above line returns true on 1st and 2nd call but doesnt spawn on 1st
Err(e) => println!("Error with stream: {}",e)
}
}
}
//start thread function
pub fn start_thread(future : impl Future<Output = ()> + Send + 'static) -> bool{
match GLOBAL_RUNTIME.get_or_init(|| Runtime::new().map_or(None, |x| Some(x))){
Some(runtime) => {
runtime.spawn(future);
true
//this returns true every time but doesnt actually spawn future first time is called
}
None => false
}
}
//GLOBAL_RUNTIME
static GLOBAL_RUNTIME: OnceLock<Option<Runtime>> = OnceLock::new();
Again this works fine on rayon with 1st and all future tcp streams being returned (call rayon::spawn directly from functions for that), guessing is a problem with using a global runtime, anyone know why this not working?