#Help with multi threaded code

17 messages · Page 1 of 1 (latest)

thorn violet
#

https://github.com/dev-ardi/HN_aggregator

I have some questions about this:

  1. General advice of things that could be done better
  2. why is creating a reqwest::Client so slow (7-9ms)?
  3. Having to manually drop tx and rx feels like I've done an anti pattern somewhere
  4. If I don't call exit(0) the program hangs for a few seconds

I wonder if instead of

    while let Ok(story) = rx.recv() {
        sorted_stories.push(Pair(story.score.unwrap_or_default(), story.id));
        stories.insert(story.id, story);
    }
    drop(rx);

I could do it in another task (for some reason)

    let (sorted_stories, stories) = tokio::spawn(async move {
        while let Ok(story) = rx.recv() {
            sorted_stories.push(Pair(story.score.unwrap_or_default(), story.id));
            stories.insert(story.id, story);
        }
        (sorted_stories, stories)
    })
    .await
    .unwrap();

I don't know how to get rid of the drop(tx) though (I could use a {} block but that's not what I mean

GitHub

Hacker News Client. Contribute to dev-ardi/HN_aggregator development by creating an account on GitHub.

thorn violet
#

I'm especially confused about the call to exit()

shrewd beacon
#

my guess is that a task is still running that has not been closed, so the runtime can't finish

#

the code finishes for me without the exit

#

it might be more natural to make the for where you do the setup and the while where you receive the results in different functions, so the sender/receiver will go out of the scope at the end of it, so no manual drop

#

I have no good evidence for this, but on my system at least Client::new does a bunch of calls to openssl, which reads some certificates from the system, so that could be why it takes so much?

#

that being said it's not the end of the world, you're not supposed to have too many clients anyway

#

yeah

#

all the time from Client::new is spent in some openssl function

thorn violet
thorn violet
shrewd beacon
shrewd beacon
#

I'd say the idea is to not get into that situation