My use case was running it in gtk::idle_add/timeout_add, which only supports non-blocking calls (it was run on the same thread). It was also to read from it over a mutex, which means I could not keep the mutex locked during a whole blocking read.
A GUI's event loop is very similar to to an async framework's reactor, so you were already using one behind the scenes.
for thing in things...
I said "handled by different code" - in your case they all go to the same code, the one that called that function.
Consider this:
This small program takes the number from 1 to 9, uses the math.js webservice to square them, and then uses that webservice again to multiply the result by 100. This, of course, I could do inside Rust - but I want to demonstrate an async flow, so I make them web requests - and I also added some random delay for each request to simulate real world scenarios where requests can take different times.
For each number, this program sends two requests - one for squaring it and one for multiplying it by 100. If I only wanted one request, your approach could work(pseudo-rust):
But... I do two different requests for each number, and handle their responses differently - for the first I send another request based on it's result, and for the second I store the result in a vector(actually join_all does that for me - I just log it and pass it on). How would you do that? Use two callbacks?
Alright, you're making some good points. I suppose I should see if I can use GTK+'s reactor to read a connection. Doubt it, but perhaps.
In your example though, I don't see why you couldn't just make the connection vector hold a type parameter and add the second request with another type.
In your example though, I don't see why you couldn't just make the connection vector hold a type parameter and add the second request with another type.
A GUI's event loop is very similar to to an async framework's reactor, so you were already using one behind the scenes.
I said "handled by different code" - in your case they all go to the same code, the one that called that function.
Consider this:
This small program takes the number from 1 to 9, uses the math.js webservice to square them, and then uses that webservice again to multiply the result by 100. This, of course, I could do inside Rust - but I want to demonstrate an async flow, so I make them web requests - and I also added some random delay for each request to simulate real world scenarios where requests can take different times.
For each number, this program sends two requests - one for squaring it and one for multiplying it by 100. If I only wanted one request, your approach could work(pseudo-rust):
But... I do two different requests for each number, and handle their responses differently - for the first I send another request based on it's result, and for the second I store the result in a vector(actually
join_all
does that for me - I just log it and pass it on). How would you do that? Use two callbacks?Alright, you're making some good points. I suppose I should see if I can use GTK+'s reactor to read a connection. Doubt it, but perhaps.
In your example though, I don't see why you couldn't just make the connection vector hold a type parameter and add the second request with another type.
You mean something like this?
It doesn't scale very well:
Alright. Thanks for a nice discussion!