【发布时间】:2020-07-26 08:47:47
【问题描述】:
有没有更好的方法来做类似下面的事情?
use futures::channel::oneshot; // 0.3.4
use std::thread;
pub async fn spawn<Y>(f: impl Fn() -> Y + Send + Sync + 'static) -> Y
where
Y: Send + 'static,
{
let (sender, receiver) = oneshot::channel::<Y>();
thread::spawn(move || sender.send(f()));
receiver.await.unwrap()
}
【问题讨论】:
-
欢迎来到 Stack Overflow! What is the best approach to encapsulate blocking I/O in future-rs? 的答案似乎可以回答您的问题。如果没有,请edit您的问题来解释差异。否则,我们可以将此问题标记为已回答。
-
@Shepmaster 谢谢。
tokio::task::spawn_blocking是我要找的。span>
标签: multithreading rust future