Skip to content

Commit d0953e8

Browse files
authored
task: simplify the example of TaskTracker (#7657)
1 parent b157f5d commit d0953e8

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

tokio-util/src/task/task_tracker.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ use tokio::{
9292
/// ```
9393
/// use tokio_util::sync::CancellationToken;
9494
/// use tokio_util::task::TaskTracker;
95+
/// use tokio_util::time::FutureExt;
96+
///
9597
/// use tokio::time::{self, Duration};
9698
///
9799
/// async fn background_task(num: u64) {
@@ -111,17 +113,18 @@ use tokio::{
111113
/// for i in 0..10 {
112114
/// let token = token.clone();
113115
/// tracker.spawn(async move {
114-
/// // Use a `tokio::select!` to kill the background task if the token is
115-
/// // cancelled.
116-
/// tokio::select! {
117-
/// () = background_task(i) => {
118-
/// println!("Task {} exiting normally.", i);
119-
/// },
120-
/// () = token.cancelled() => {
116+
/// // Use a `with_cancellation_token_owned` to kill the background task
117+
/// // if the token is cancelled.
118+
/// match background_task(i)
119+
/// .with_cancellation_token_owned(token)
120+
/// .await
121+
/// {
122+
/// Some(()) => println!("Task {} exiting normally.", i),
123+
/// None => {
121124
/// // Do some cleanup before we really exit.
122125
/// time::sleep(Duration::from_millis(50)).await;
123126
/// println!("Task {} finished cleanup.", i);
124-
/// },
127+
/// }
125128
/// }
126129
/// });
127130
/// }

0 commit comments

Comments
 (0)