-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
time: add an example using interval
to the time module
#2623
Conversation
Add an example using `interval` to the time module. Add the same example to the `interval` function. Copy the `delay_for` example in the module to the `delay_for` function. Finally, fix the comment for the `delay_for` example in the time module Refs: #2494
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The text here explains it as interval
allowing you to do other things while awaiting the tick, but that's not really accurate. The amount of time a .tick().await
call spends sleeping depends on the time since the last tick, but the tick still causes the task to sleep, and doesn't cause several things to happen at the same time.
We should also not use the word "blocks" for waiting, as it is easily confused with blocking the thread. I have added a suggestion below for how one might rephrase the text.
tokio/src/time/interval.rs
Outdated
/// `tokio::time::interval` yields so every iteration of the loop takes | ||
/// two seconds (except the first as the first `tick` completes immediately). | ||
/// The difference with `tokio::time::delay_for` is that `delay_for` blocks. | ||
/// Replacing `interval` with `delay_for` would result in a loop that takes | ||
/// three seconds per iteration. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is dangerous to use the phrase "blocks" because it is very easily confused with blocking the thread, which is a very bad thing to do in async/await.
/// `tokio::time::interval` yields so every iteration of the loop takes | |
/// two seconds (except the first as the first `tick` completes immediately). | |
/// The difference with `tokio::time::delay_for` is that `delay_for` blocks. | |
/// Replacing `interval` with `delay_for` would result in a loop that takes | |
/// three seconds per iteration. | |
/// The difference between `interval` and [`delay_for`] is that an `interval` | |
/// measures the time since the last tick, which means that `.tick().await` | |
/// may wait for a shorter time than the duration specified for the interval | |
/// if some time has passed between calls to `.tick().await`. | |
/// | |
/// If the tick in the example below was replaced with `delay_for`, the task | |
/// would only be executed once every three seconds, and not every two | |
/// seconds. | |
/// | |
/// [`delay_for`]: crate::time::delay_for() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, that is much better. Thank you. I'll update the comment in the module as well and add links.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also added links to interval
in the docs for interval
. Is that too much?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is unnecessary to make it a link on interval
's documentation.
tokio/src/time/delay.rs
Outdated
/// Wait 100ms and print "100 ms have elapsed". Note that no work is | ||
/// performed while awaiting on the delay to complete. If you wish to do | ||
/// other work while awaiting please see [`interval`]. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you change it so it says that interval
is used for regularly running something according to a schedule or something like that? And move the part about interval
up before the # Cancellation
header?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How is the following?
If you wish to run something regularly on a schedule see [
interval
].
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer to avoid "If you wish" in documentation, but other than that it is fine
To run something regularly on a schedule, see
interval
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay! Thanks.
Remove links to `interval` in the `interval` documentation and reword the comment for `delay_for`.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the PR!
- docs: misc improvements (#2572, #2658, #2663, #2656, #2647, #2630, #2487, #2621, #2624, #2600, #2623, #2622, #2577, #2569, #2589, #2575, #2540, #2564, #2567, #2520, #2521, #2572, #2493) - rt: allow calls to `block_on` inside calls to `block_in_place` that are themselves inside `block_on` (#2645) - net: fix non-portable behavior when dropping `TcpStream` `OwnedWriteHalf` (#2597) - io: improve stack usage by allocating large buffers on directly on the heap (#2634) - io: fix unsound pin projection in `AsyncReadExt::read_buf` and `AsyncWriteExt::write_buf` (#2612) - io: fix unnecessary zeroing for `AsyncRead` implementors (#2525) - io: Fix `BufReader` not correctly forwarding `poll_write_buf` (#2654) - coop: returning `Poll::Pending` no longer decrements the task budget (#2549) - io: little-endian variants of `AsyncReadExt` and `AsyncWriteExt` methods (#1915) - io: fix panic in `AsyncReadExt::read_line` (#2541) - task: add [`tracing`] instrumentation to spawned tasks (#2655) - sync: allow unsized types in `Mutex` and `RwLock` (via `default` constructors) (#2615) - net: add `ToSocketAddrs` implementation for `&[SocketAddr]` (#2604) - fs: add `OpenOptionsExt` for `OpenOptions` (#2515) - fs: add `DirBuilder` (#2524) [`tracing`]: https://crates.io/crates/tracing Signed-off-by: Eliza Weisman <eliza@buoyant.io>
# 0.2.22 (July 2!, 2020) ### Fixes - docs: misc improvements (#2572, #2658, #2663, #2656, #2647, #2630, #2487, #2621, #2624, #2600, #2623, #2622, #2577, #2569, #2589, #2575, #2540, #2564, #2567, #2520, #2521, #2493) - rt: allow calls to `block_on` inside calls to `block_in_place` that are themselves inside `block_on` (#2645) - net: fix non-portable behavior when dropping `TcpStream` `OwnedWriteHalf` (#2597) - io: improve stack usage by allocating large buffers on directly on the heap (#2634) - io: fix unsound pin projection in `AsyncReadExt::read_buf` and `AsyncWriteExt::write_buf` (#2612) - io: fix unnecessary zeroing for `AsyncRead` implementors (#2525) - io: Fix `BufReader` not correctly forwarding `poll_write_buf` (#2654) - io: fix panic in `AsyncReadExt::read_line` (#2541) ### Changes - coop: returning `Poll::Pending` no longer decrements the task budget (#2549) ### Added - io: little-endian variants of `AsyncReadExt` and `AsyncWriteExt` methods (#1915) - task: add [`tracing`] instrumentation to spawned tasks (#2655) - sync: allow unsized types in `Mutex` and `RwLock` (via `default` constructors) (#2615) - net: add `ToSocketAddrs` implementation for `&[SocketAddr]` (#2604) - fs: add `OpenOptionsExt` for `OpenOptions` (#2515) - fs: add `DirBuilder` (#2524) [`tracing`]: https://crates.io/crates/tracing Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Motivation
To add an
interval
example to the time module, add also add examples to theinterval
anddelay_for
functions. (#2494).Solution
interval
to the time moduleinterval
functiondelay_for
example in the module to thedelay_for
functiondelay_for
example in the moduleRefs: #2494