1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-29 16:30:56 -05:00

use single thread runime in tokio_util::block_on (#3080)

This commit is contained in:
Bartek Iwańczuk 2019-10-07 10:31:56 +02:00 committed by Ryan Dahl
parent e1d49fe0fe
commit 41ed4cd34e

View file

@ -36,8 +36,10 @@ where
/// THIS IS A HACK AND SHOULD BE AVOIDED. /// THIS IS A HACK AND SHOULD BE AVOIDED.
/// ///
/// This creates a new tokio runtime, with many new threads, to execute the /// This spawns a new thread and creates a single-threaded tokio runtime on that thread,
/// given future. This is useful when we want to block the main runtime to /// to execute the given future.
///
/// This is useful when we want to block the main runtime to
/// resolve a future without worrying that we'll use up all the threads in the /// resolve a future without worrying that we'll use up all the threads in the
/// main runtime. /// main runtime.
pub fn block_on<F, R>(future: F) -> Result<R, ErrBox> pub fn block_on<F, R>(future: F) -> Result<R, ErrBox>
@ -50,10 +52,7 @@ where
let (sender, receiver) = channel(); let (sender, receiver) = channel();
// Create a new runtime to evaluate the future asynchronously. // Create a new runtime to evaluate the future asynchronously.
thread::spawn(move || { thread::spawn(move || {
let r = match create_threadpool_runtime() { let r = tokio::runtime::current_thread::block_on_all(future);
Ok(mut rt) => rt.block_on(future),
Err(e) => Err(ErrBox::from(e)),
};
sender sender
.send(r) .send(r)
.expect("Unable to send blocking future result") .expect("Unable to send blocking future result")