Skip to content

Instantly share code, notes, and snippets.

@Eliah-Lakhin
Created August 13, 2019 15:49
Show Gist options
  • Save Eliah-Lakhin/9514b4798a2aed84615dbde807dbafa5 to your computer and use it in GitHub Desktop.
Save Eliah-Lakhin/9514b4798a2aed84615dbde807dbafa5 to your computer and use it in GitHub Desktop.
Waking up Tokio Blocking Thread attempt
#![feature(async_await)]
use std::error::Error;
use tokio::spawn;
use tokio::prelude::*;
use tokio::future::poll_fn;
use tokio_threadpool::blocking;
use tokio::sync::oneshot::{Sender, Receiver, channel};
#[tokio::main]
async fn main() {
let (init_tx, mut init_rx) = channel::<(usize, Sender<bool>)>();
spawn(async move {
poll_fn(|_| blocking(|| {
let f = async {
println!("In thread");
// A point of failure
let (x, done_tx) = init_rx.try_recv().unwrap();
println!("X received: {}", x);
done_tx.send(true);
};
tokio_executor::enter().unwrap().block_on(f);
})).await;
});
// Removing this line makes code work
std::thread::sleep(std::time::Duration::from_secs(5));
println!("Before init");
let (done_tx, done_rx) = channel();
init_tx.send((100, done_tx)).map_err(|_| panic!("Failed to send"));
println!("After init");
done_rx.await;
}
@Eliah-Lakhin
Copy link
Author

Eliah-Lakhin commented Aug 13, 2019

An error:

thread 'tokio-runtime-worker-0' panicked at 'called `Result::unwrap()` on an `Err` value: EnterError { reason: "attempted to run an executor while another executor is already running" }', src/libcore/result.rs:1084:5

tokio, tokio-threadpool, tokio-executor versions is 0.2.0-alpha.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment