Skip to content

Commit

Permalink
rt: add Handle::block_on
Browse files Browse the repository at this point in the history
  • Loading branch information
carllerche committed Feb 23, 2021
1 parent c9d2a36 commit ef56cd8
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions tokio/src/runtime/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,18 @@ impl Handle {
let _ = self.blocking_spawner.spawn(task, &self);
handle
}

/// TODO: write docs if this is a good direction
pub fn block_on<F: Future>(&self, future: F) -> F::Output {
// Enter the **runtime** context. This configures spawning, the current I/O driver, ...
let _rt_enter = self.enter();

// Enter a **blocking** context. This prevents blocking from a runtime.
let mut _blocking_enter = crate::runtime::enter(true);

// Block on the future
_blocking_enter.block_on(future).expect("failed to park thread")
}
}

/// Error returned by `try_current` when no Runtime has been started
Expand Down

0 comments on commit ef56cd8

Please sign in to comment.