Asking for user input in async task

I need to iterate over an array of objects, each of which requires a call to an async function. For each object I want to ask the user to accept, reject or ignore.

Probably a really stupid question, but here it goes: How does one ask for user input in the terminal in an async task (using tokio, in case it matters)?

What are you expecting the user interface to be like? If you just want to print out "What do you want to do with {object}? [accept/reject/ignore]" for each object, one at time, look into tokio::io::stdout and tokio::io::stdin (though, depending on whether this prompt is run within concurrent tasks, you may need to set up a mutex to ensure no two tasks prompt at the same time).

I had some distinct memory of having tried to use std's Stdin::read_line() from a spawn_blocking() previously with somewhat disastrous results. But when I tried it now, it seems to be working fine. Not sure what I'm doing differently (or if this was a platform-specific issue), but that's basically what I wanted -- and apparently have.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.