如何将任务添加到被另一个线程上运行的东京事件循环?

问题描述 投票:2回答:1

I'd like to旋转了一个东京事件循环旁边一个火箭服务器,然后添加事件,这个循环以后。我读Is there a way to launch a tokio::Delay on a new thread to allow the main loop to continue?,但它仍然不是很清楚,我如何实现我的目标。

rust future rust-tokio
1个回答
2
投票

作为the documentation states

虽然current_thread::Runtime没有实现Send,不能安全地移动到其他线程,它提供了可以发送到其他线程,并允许从那里产生新的任务Handle

这里是一个线程起转事件循环和其上具有第二螺纹产卵任务的一个例子。任务都开始在错开的时间间隔,但在同一时间完成:

use std::{
    thread,
    time::{Duration, Instant},
};
use tokio::{prelude::*, runtime::current_thread, timer::Delay}; // 0.1.15

fn main() {
    let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel();
    let (handle_tx, handle_rx) = std::sync::mpsc::channel();

    let tokio_thread = thread::spawn(move || {
        let mut runtime = current_thread::Runtime::new().expect("Unable to create the runtime");

        eprintln!("Runtime created");

        // Give a handle to the runtime to another thread.
        handle_tx
            .send(runtime.handle())
            .expect("Unable to give runtime handle to another thread");

        // Continue running until notified to shutdown
        runtime
            .spawn({ shutdown_rx.map_err(|e| panic!("Error on the shutdown channel: {:?}", e)) });

        // Finish all pending tasks
        runtime.run().expect("Unable to run the runtime");

        eprintln!("Runtime finished");
    });

    let another_thread = thread::spawn(move || {
        let handle = handle_rx
            .recv()
            .expect("Could not get a handle to the other thread's runtime");

        eprintln!("Another thread created");
        let two_seconds_after_creation = Instant::now() + Duration::from_secs(2);

        for value in 0..10 {
            // Run this future in the other thread's runtime
            handle
                .spawn(future::lazy(move || {
                    eprintln!("Starting task for value {}", value);

                    Delay::new(two_seconds_after_creation)
                        .inspect(move |_| eprintln!("Finishing task for value {}", value))
                        .map(drop)
                        .map_err(drop)
                }))
                .expect("Unable to spawn a new task on the runtime");

            thread::sleep(Duration::from_millis(100));
        }

        eprintln!("Another thread finished");
    });

    another_thread.join().expect("Another thread panicked");

    shutdown_tx
        .send(())
        .expect("Unable to shutdown runtime thread");

    tokio_thread.join().expect("Tokio thread panicked");
}
Runtime created
Another thread created
Starting task for value 0
Starting task for value 1
Starting task for value 2
Starting task for value 3
Starting task for value 4
Starting task for value 5
Starting task for value 6
Starting task for value 7
Starting task for value 8
Starting task for value 9
Another thread finished
Finishing task for value 0
Finishing task for value 1
Finishing task for value 2
Finishing task for value 3
Finishing task for value 4
Finishing task for value 5
Finishing task for value 6
Finishing task for value 7
Finishing task for value 8
Finishing task for value 9
Runtime finished
© www.soinside.com 2019 - 2024. All rights reserved.