Red

Red

Rust Brain Teasers: Comments on puzzle 19/async (page 82-)

An open discussion about this particular puzzle on asynchronous code, since I’m only learning and I may have overlooked something.

1.) About tokio, you either have to declare the main as

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { ... }

or if main is something else, it also possible to use its own block_on (or other alternative) to launch the asynchronous code. In this case, you have to enable the time “reactor” to avoid a panic:

fn main() {
    let rt = runtime::Builder::new_multi_thread()
        .enable_time()
        .build()
        .unwrap();

    rt.block_on(test()).unwrap();
}

with, for example

    async fn test() -> Result<(), Box<dyn std::error::Error>> {
        join!(count_and_wait(1), count_and_wait(2), count_and_wait(3));
        Ok(())
    }

or directly inserting an async block: rt.block_on(async { ... }).

2.) There is another alternative to using tokio::time::sleep or a thread-oriented approach.

The problem comes from using a thread function, which is not meant for this sort of asynchronous pattern as very well explained pages 82 and 85. We actually get the same issue in other languages like Kotlin. There is no .await and so no real “break” that will allow to switch to another async code.

There is an asynchronous async_std::task::sleep function which supports .await and will give the expected outcome:

    use async_std::task;

    async fn count_and_wait(n: u64) -> u64 {
        println!("starting {}", n);
        task::sleep(Duration::from_millis(n * 100)).await;
        println!("returning {}", n);
        n
    }

Popular Prag Prog topics Top

jon
Some minor things in the paper edition that says “3 2020” on the title page verso, not mentioned in the book’s errata online: p. 186 But...
New
Razor54672
The answer to 3rd Problem of Chapter 5 (Making Choices) of “Practical Programming, Third Edition” seems incorrect in the given answer ke...
New
jesse050717
Title: Web Development with Clojure, Third Edition, pg 116 Hi - I just started chapter 5 and I am stuck on page 116 while trying to star...
New
Alexandr
Hi everyone! There is an error on the page 71 in the book “Programming machine learning from coding to depp learning” P. Perrotta. You c...
New
simonpeter
When I try the command to create a pair of migration files I get an error. user=&gt; (create-migration "guestbook") Execution error (Ill...
New
mikecargal
Title: Hands-on Rust: question about get_component (page 295) (feel free to respond. “You dug you’re own hole… good luck”) I have somet...
New
gilesdotcodes
In case this helps anyone, I’ve had issues setting up the rails source code. Here were the solutions: In Gemfile, change gem 'rails' t...
New
adamwoolhether
When trying to generate the protobuf .go file, I receive this error: Unknown flag: --go_opt libprotoc 3.12.3 MacOS 11.3.1 Googling ...
New
oaklandgit
Hi, I completed chapter 6 but am getting the following error when running: thread 'main' panicked at 'Failed to load texture: IoError(O...
New
dachristenson
I’ve got to the end of Ch. 11, and the app runs, with all tabs displaying what they should – at first. After switching around between St...
New

Other popular topics Top

AstonJ
A thread that every forum needs! Simply post a link to a track on YouTube (or SoundCloud or Vimeo amongst others!) on a separate line an...
New
axelson
I’ve been really enjoying obsidian.md: It is very snappy (even though it is based on Electron). I love that it is all local by defaul...
New
Exadra37
On modern versions of macOS, you simply can’t power on your computer, launch a text editor or eBook reader, and write or read, without a ...
New
New
Exadra37
I am a Linux user since 2012, more or less, and I always use Ubuntu on my computers, and my last 2 laptops have been used Thinkpads, wher...
New
Rainer
Not sure if following fits exactly this thread, or if we should have a hobby thread… For many years I’m designing and building model air...
New
AstonJ
In case anyone else is wondering why Ruby 3 doesn’t show when you do asdf list-all ruby :man_facepalming: do this first: asdf plugin-upd...
New
rustkas
Intensively researching Erlang books and additional resources on it, I have found that the topic of using Regular Expressions is either c...
New
PragmaticBookshelf
Author Spotlight Erin Dees @undees Welcome to our new author spotlight! We had the pleasure of chatting with Erin Dees, co-author of ...
New
PragmaticBookshelf
Author Spotlight: Bruce Tate @redrapids Programming languages always emerge out of need, and if that’s not always true, they’re defin...
New

Latest in PragProg

View all threads ❯