I am trying to grok the basic concepts of async (Future, poll, Wake...). To keep it simple I thought maybe I avoid using
async-std and just use what std and
futures crates provide.
Let's say you have an expensive computation that needs to run for a long time and you'd like to report back the progress of computation, say, at 50, 75, 90 and 100%.
This is me thinking in an
- main thread/executor creates the
Futurethat's responsible to run the heavy computation:
Futurespawns a new thread to actually run
- main thread
poll()s the future to get the current progress value:
- main thread checks
progress_percentand if is not
100, waits to receive
Futurein order to go to step 3.
progress_percentis 100, terminate everything.
However, reading the the (unfinished) async book, I realize that
Futures can only tell main thread to wait (
Poll::Pending) or return a value (
Poll::Ready(value)) and essentially be done (no more polling)
Additionally, the docs say this about polling:
...Instead, the current task is scheduled to be woken up when it's possible to make further progress by
I am confused about:
- make further progress! But wasn't heavy computation already making progress toward completion?!
- I guess the wording (and more likely my English) is source of confusion:
wake(), so it's already awake so why does it need to be scheduled to be woken? Isn't it better to say that Future is waking up the executor so it comes back to check on
Futureand get some result back?
So, do I have to create a new
Future every time I get a non-100 report from
poll(), and somehow handle linking the new
Future to the already running computation thread to get further progress report?
How would you do it?
Is this type of problem not suitable for
I guess similar question can be framed for a
socket that's downloading a file from a client and we want to see remain download size WITHOUT fully waiting for
Future to complete.