nbsdx•

I've mentioned this before, but I really think any usage of std::thread::* inside an async block should be a clippy lint, or even a straight up compiler warning since you most likey don't want the behavior that will be exhibited.

38
hwchenOP•

Author here. I've seen several threads on this subreddit asking about why async/await doesn't "work" as expected, where std::thread::sleep is blocking. So I decided to write a blog post in case that will help people in the future.

Corrections, feedback are welcome.

I've also filed some issues in the async book and clippy, since this seems like a pretty easy thing to trip over.

https://github.com/rust-lang/async-book/issues/64

https://github.com/rust-lang/rust-clippy/issues/4377

10
justfordc•

After reading your post, I thought I'd try finally going through the async book.

One of the introductory lines is:

Whereas calling a blocking function in a synchronous method would block the whole thread, blocked Futures will yield control of the thread, allowing other Futures to run.

This seemingly contradicts your take. Are there two different definitions of "blocking" in play here? Is the book referring to the thread-pool scenario? Or am I just misunderstanding the issue? :)

5
hwchenOP•

If I'm understanding correctly, the book is trying to contrast:

  1. A blocking call in a sync method v.
  2. A "blocked" future in an async fn.

Perhaps "blocking" has two common usages. One is more general, which I think the async book is using. In that case, "blocking" just means that the expected value is not available yet, and how to wait for the "blocked" value depends on whether the system is sync or async.

The other common usage, which you might see more around here, is "blocking" as a sync operation which doesn't allow other operations to proceed on the thread. In this usage, "non-blocking" is async.

Does this clarify?

10
justfordc•

That makes sense -- a blocked future does not necessarily block the thread it is running on. But async doesn't save you if you invoke a method that does block the thread.

e: Also, I realize now that the line from the book lends itself to misinterpretation. It contrasts calling blocking methods in a sync function to blocked Futures in an async function. It's easy to read that as comparing the same action (e.g. calling thread::sleep) in both contexts.

7
rampant_elephant•

Yes, that sounds right.

I was wondering how to explain it succinctly, how about this: async allows one thread to make progress on multiple tasks, but if one task blocks the thread, then it can’t make progress in any task.

Or more concretely: async allows a thread to switch between different tasks, but it can’t switch if it is blocked.

1
hwchenOP•

I agree; I had almost mentioned that in my reply, but didn't want to make it too long. It's probably worth an issue or PR.

1
Nokel81•

Do you actually need an executor for an async version of std::thread::sleep? Wouldn't it be possible have a future that only returns ready after the time has elapsed?

2
hwchenOP•

Even futures that don't appear to be doing anything still have to be run on an executor. What will let the async sleep know that it's time to wake up?

Everything futures related has to be run on something that keeps track of what's ready and what's not.

8
Nokel81•

Ah so even just a blocking loop is an executor?

1
hwchenOP•

I don't know if I'd say "just" a blocking loop is an executor. There's definitely more machinery around it. The async book has an example executor:

https://rust-lang.github.io/async-book/02_execution/04_executor.html

3
desiringmachines•

There's some confusion here between whats usually called an executor and a reactor.

When futures are polled, they are either ready or pending. Executors can keep polling them after they are pending, but good executors try to avoid polling more than they have to. This is why executors pass futures a waker, which the future can use to tell the executor that it should poll this future again.

A reactor is responsible for actually waking the executor. For IO, the IO reactor wakes the executor when the IO is ready to be performed. Timers are also a kind of reactor: they need to know when the duration has passed, so that they can wake the executor.

A future which simply checked whether an instant was in the past and returned ready or pending would be "correct," but it would almost certainly just block forever in any well-implemented executor, because executors don't poll the futures again unless they are woken up by something. You need a "timer reactor" which properly wakes the executor.

6
Ununoctium117•

tokio has this via Delay and a few other helpful timer methods! But, IIRC, it was very non-trivial to implement.

1
auterium•

Actually, it is very trivial to do so: tokio::timer::delay(std::time::Duration::from_secs(2)).await

2
Ununoctium117•

Oh it's incredibly easy to use, but the behind-the-scenes implementation was difficult to make efficient. I remember reading some blog posts or PRs about the different ways to schedule the wakeups, and how to efficiently handle multiple simultaneously active timers.

3
Ralith•

The implementation doesn't have to be complex, but tokio does a lot of work to make things as efficient as possible.

2