我阅读了tokio文档,我想知道将来封装昂贵的同步I/O的最佳方法是什么.
使用reactor框架,我们可以获得绿色线程模型的优势:一些OS线程通过执行程序处理大量并发任务.
未来的tokio模型是需求驱动的,这意味着未来本身将轮询其内部状态以提供有关其完成的信息; 允许背压和取消功能.据我了解,未来的投票阶段必须是非阻塞才能运作良好.
I/OI想要封装可以看作是一个长期的原子和昂贵的操作.理想情况下,独立任务将执行I/O,并且相关联的未来将轮询I/O线程以获得完成状态.
我看到的两个唯一选择是:
poll在将来的功能中.据我所知,这两种解决方案都不是最优的,并且没有充分利用绿色线程模型(首先不在文档中建议,其次不通过reactor框架提供的执行程序).还有其他解决方案吗?
我rust_bert用于总结文本。我需要用 设置一个模型rust_bert::pipelines::summarization::SummarizationModel::new,它从互联网上获取模型。它以异步方式执行此操作,tokio并且(我认为)我遇到的问题是我正在另一个 Tokio 运行时中运行 Tokio 运行时,如错误消息所示:
Downloading https://cdn.huggingface.co/facebook/bart-large-cnn/config.json to "/home/(censored)/.cache/.rustbert/bart-cnn/config.json"
thread 'main' panicked at 'Cannot start a runtime from within a runtime. This happens because a function (like `block_on`) attempted to block the current thread while the thread is being used to drive asynchronous tasks.', /home/(censored)/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.21/src/runtime/enter.rs:38:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Run Code Online (Sandbox Code Playgroud)
我试过与模型同步运行
tokio::task::spawn_blocking
,tokio::task::block_in_place
但它们都不适合我。block_in_place给出了同样的错误,就像是不存在的,并spawn_blocking没有真正似乎是用我的。我也尝试过summarize_text异步,但这并没有多大帮助。Github 问题
tokio-rs/tokio#2194
和 Reddit 发布 …
我有一个特征,我用它来抽象tokio::net::TcpStream和tokio::net::UnixStream:
/// Interface for TcpStream and UnixStream.
trait TryRead {
// overlapping the name makes it hard to work with
fn do_try_read(&self, buf: &mut [u8]) -> Result<usize, std::io::Error>;
}
impl TryRead for TcpStream {
fn do_try_read(&self, buf: &mut [u8]) -> Result<usize, std::io::Error> {
self.try_read(buf)
}
}
Run Code Online (Sandbox Code Playgroud)
问题是我想pub async fn readable(&self) -> io::Result<()>在这两种方法中都抽象出来,但是无法在特征中实现异步方法。我该如何处理?