zlacker

[parent] [thread] 7 comments
1. doakes+(OP)[view] [source] 2024-01-20 01:27:12
But at what point are you calling await?

If I were using the author's library, I would call `.some_endpoint(...)` and that would return a `SpotifyResult<String>`, so I'm struggling to understand why `some_endpoint` is async. I could see if two different threads were calling `some_endpoint` then awaiting would allow them to both use resources, but if you're running two threads, doesn't that already accomplish the same thing? I'm pretty naive to concurrency.

replies(3): >>sodali+t >>roywig+12 >>fshbbd+w4
2. sodali+t[view] [source] 2024-01-20 01:31:12
>>doakes+(OP)
I was calling await on ws_stream.next() - awaiting for there to be a new message.

Code: https://gist.github.com/sigaloid/d0e2a7eb42fed8c2397fbf84239...

In the example you give, yes, it's just sequential and everything relies on a previous thing. But say you are making a spotify frontend - you want to render a list of playlists, the profile info, etc - you call await on all of them and they can complete simultaneously.

3. roywig+12[view] [source] 2024-01-20 01:46:05
>>doakes+(OP)
If you are only ever doing one thing at a time, then async doesn't do you any good, which is why people like to use sync libraries when they can't make use of it, and why libraries might want to provide a sync and an async version.

Async is useful when you want to have a bunch of things happening (approximately) "at the same time" on a single thread.

With async you can await on two different SpotifyResults at the same time without multithreading. When each one is ready, the runtime will execute the remainder of the function that was awaiting. This means the actual HTTP requests can be in flight at the same time.

replies(1): >>doakes+k8
4. fshbbd+w4[view] [source] 2024-01-20 02:11:25
>>doakes+(OP)
A common usecase for async is if you are implementing a web service with an API. Suppose your API uses Spotify’s API. Your server can handle many requests at once. It would be nice if all of them can call Spotify’s API at the same time without each holding a thread. Tokio tasks have much lower overhead than OS threads. Your N requests can all await at once and it doesn’t take N threads to do it.
◧◩
5. doakes+k8[view] [source] [discussion] 2024-01-20 02:57:30
>>roywig+12
I guess I'm just confused that it's labeled async (and does async work) but is actually blocking when invoked (or at least doesn't return until await finishes).

If I'm awaiting on two different results, I have to invoke them in parallel somehow, right? What is that mechanism and why doesn't that already provide asynchrony? Like, if the method was sync, couldn't I still run it async somehow?

replies(3): >>mplanc+yb >>Too+Np >>SkiFir+sz
◧◩◪
6. mplanc+yb[view] [source] [discussion] 2024-01-20 03:35:07
>>doakes+k8
This is sort of the core of the whole idea of concurrency. There are lots of good explanations online if you search for “concurrency vs parallelism.”

The gist is that while you await the result of an async function, you yield to the executor, which is then free to work on other tasks until whatever the await is waiting for has completed.

The group of tasks being managed by the executor is all different async functions, which all yield to the executor at various times when they are waiting for some external resource in order to make forward progress, to allow others to make progress in the meantime.

This is why people say it’s good for IO-bound workloads, which spend the majority of their time waiting for external systems (the disk, the network, etc)

◧◩◪
7. Too+Np[view] [source] [discussion] 2024-01-20 06:44:43
>>doakes+k8
You might be awaiting for only one result, but the one calling you may be awaiting both you and 100 other things that you are not aware of.
◧◩◪
8. SkiFir+sz[view] [source] [discussion] 2024-01-20 09:17:40
>>doakes+k8
> I guess I'm just confused that it's labeled async (and does async work) but is actually blocking when invoked (or at least doesn't return until await finishes).

From the point of view of the `async` block/function, it is blocking, but from the point of view of the thread executing that `async` block/function it is not.

> If I'm awaiting on two different results, I have to invoke them in parallel somehow, right?

No, the whole point of `async` is having concurrency (i.e. multiple tasks running and interleaving) without necessarily using parallelism for it. Take a look at the `join` macro in the `futures` crate for example, it allows you to `.await` two futures while allowing both of them to make progress (two successive `.await`s would force the first one to end before the second one can start) and without spawning dedicated threads for them.

[go to top]