Search code examples
rustrust-tokio

Tokio thread is not starting / spawning


I'm trying to start a new task to read from a socket client. I'm using the following same method on both the websocket server and client to receive from the connection.

The problem is, on the server side, the thread is started (2 log lines printed), but on the client side the thread is not starting (only the first line printed).

If I await on the spawn(), I can receive from the client. But then the parent task cannot proceed.

Any pointers for solving this problem?

  pub async fn receive_message_from_peer(
    mut receiver: PeerReceiver,
    sender: Sender<IoEvent>,
    peer_index: u64,
) {
    debug!("starting new task for reading from peer : {:?}", peer_index);
    tokio::task::spawn(async move {
        debug!("new thread started for peer receiving");
     // ....
    }); // not awaiting or join!()

Solution

  • So I think the issue was I was using std::thread::sleep() in async code in some places. after using tokio::time::sleep() didn't need to yield the thread.