Skip to content
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

Merged
merged 3 commits into from
Jun 16, 2020
Merged

time: add an example using interval to the time module #2623

merged 3 commits into from
Jun 16, 2020

Conversation

craigpastro
Copy link
Contributor

Motivation

To add an interval example to the time module, add also add examples to the interval and delay_for functions. (#2494).

Solution

  • 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
  • Fix the comment for the delay_for example in the module

Refs: #2494

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
Copy link
Contributor

@Darksonn Darksonn left a 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.

Comment on lines 40 to 44
/// `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.
Copy link
Contributor

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.

Suggested change
/// `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()

Copy link
Contributor Author

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.

Copy link
Contributor Author

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?

Copy link
Contributor

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.

@Darksonn Darksonn added A-tokio Area: The main tokio crate C-maintenance Category: PRs that clean code up or issues documenting cleanup. M-time Module: tokio/time T-docs Topic: documentation labels Jun 16, 2020
Comment on lines 39 to 41
/// 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`].
Copy link
Contributor

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?

Copy link
Contributor Author

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].

Copy link
Contributor

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.

Copy link
Contributor Author

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`.
Copy link
Contributor

@Darksonn Darksonn left a 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!

@Darksonn Darksonn merged commit e2adf26 into tokio-rs:master Jun 16, 2020
@craigpastro craigpastro deleted the interval-example branch June 16, 2020 09:59
hawkw added a commit that referenced this pull request Jul 20, 2020
- 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>
hawkw added a commit that referenced this pull request Jul 22, 2020
# 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-maintenance Category: PRs that clean code up or issues documenting cleanup. M-time Module: tokio/time T-docs Topic: documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants