- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在创建一个将产生其他任务的任务。其中一些需要一些时间,因此无法等待,但可以并行运行:
src/main.rs
use crossbeam::crossbeam_channel::{bounded, select};
#[tokio::main]
async fn main() {
let (s, r) = bounded::<usize>(1);
tokio::spawn(async move {
let mut counter = 0;
loop {
let loop_id = counter.clone();
tokio::spawn(async move { // why this one was not fired?
println!("inner task {}", loop_id);
}); // .await.unwrap(); - solves issue, but this is long task which cannot be awaited
println!("loop {}", loop_id);
select! {
recv(r) -> rr => {
// match rr {
// Ok(ee) => {
// println!("received from channel {}", loop_id);
// tokio::spawn(async move {
// println!("received from channel task {}", loop_id);
// });
// },
// Err(e) => println!("{}", e),
// };
},
// more recv(some_channel) ->
}
counter = counter + 1;
}
});
// let s_clone = s.clone();
// tokio::spawn(async move {
// s_clone.send(2).unwrap();
// });
loop {
// rest of the program
}
}
我注意到奇怪的行为。这输出:
loop 0
我期待它也能输出
inner task 0
.
loop 0
inner task 0
loop 1
缺少
inner task 1
.
inner task
产生一个延迟循环?
tokio::spawn
对另一个,只有最后一个会有这个问题。调用
tokio::spawn
时有什么需要注意的吗?和
select!
?是什么导致这一循环延迟?
[dependencies]
tokio = { version = "0.2", features = ["full"] }
crossbeam = "0.7"
使用rust 1.46,Windows 10
最佳答案
select!
正在阻塞,tokio::spawn
的文档say :
The spawned task may execute on the current thread, or it may be sent to a different thread to be executed.
select!
“future”实际上是一个阻塞函数,而
spawn
不使用新线程(在第一次调用或循环内)。
tokio::task::spawn_blocking
对于
select!
-ing 关闭(这将不再是 future ,所以
async move {}
现在是
move || {}
)。
use crossbeam::crossbeam_channel::{bounded, select};
#[tokio::main]
async fn main() {
let (s, r) = bounded::<usize>(1);
tokio::task::spawn_blocking(move || {
// ...
});
loop {
// rest of the program
}
}
Link to playground
tokio::sync::mpsc
这样的非阻塞 channel 。 , 你可以在上面使用
await
并获得预期的行为,例如
playground example直接
recv().await
或与
tokio::select!
, 像这样:
use tokio::sync::mpsc;
#[tokio::main]
async fn main() {
let (mut s, mut r) = mpsc::channel::<usize>(1);
tokio::spawn(async move {
loop {
// ...
tokio::select! {
Some(i) = r.recv() => {
println!("got = {}", i);
}
}
}
});
loop {
// rest of the program
}
}
Link to playground
关于asynchronous - 为什么在 crossbeam_channel::select 旁边调用时 tokio::spawn 有延迟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64126518/
当运行这样的代码时: use futures::executor; ... pub fn store_temporary_password(email: &str, password: &str) -
我遵循了mdns Rust文档并粘贴了示例代码,但它抛出了以下错误:。以下是我拥有的代码:。依赖关系:。我遗漏了什么?我试着在网上寻找,但没有找到如何为这个用例创建一个反应堆。
假设我想与 Tokio 同时下载两个网页... 要么我可以用 tokio::spawn() 来实现这个: async fn v1() { let t1 = tokio::spawn(reqwe
我制作了一个还能显示天气的 LED 时钟。我的程序在一个循环中做了几件不同的事情,每件事都有不同的间隔: 每 50 毫秒更新一次 LED, 每 1 秒检查一次光照水平(以调整亮度), 每 10 分钟获
我制作了一个还能显示天气的 LED 时钟。我的程序在一个循环中做了几件不同的事情,每件事都有不同的间隔: 每 50 毫秒更新一次 LED, 每 1 秒检查一次光照水平(以调整亮度), 每 10 分钟获
tokio::run_async + futures 0.3 + tokio::net::UnixStream panic 。 设置 [package] name = "prac" version =
在我的 rust 项目中,cargo 提示使用 tokio::sync 时使用的类型不在范围内: use tokio::sync::RwLock; | ^^^^^ use of undec
我将如何使用自定义 tokio 运行时构建器并且没有主宏来实现这个 tokio_postgres 示例? 这工作正常,根据 tokio_postgres docs : 示例/withmacro.rs
目前我有一个主要的写成 async example for the Reqwest library . #[tokio::main] async fn main() -> Result> { 我们可以
我遵循the mdns Rust documentation并粘贴了示例代码,但它引发以下错误: thread 'main' panicked at 'there is no reactor runn
extern crate tokio; // 0.1.22 use tokio::io; use tokio::net::TcpListener; use tokio::prelude::*; use
我正在尝试使用 tokio 编写一个测试程序,该程序从网站获取文件并将流式响应写入文件。 hyper 网站显示了一个使用 while 循环并使用 .data() 的示例。方法响应主体,但我想用 .ma
我在 prod 中运行一个 rust Tokio 应用程序。在上一个版本中,我有一个错误,一些请求导致我的代码进入无限循环。 发生的事情是当进入无限循环的任务卡住时,所有其他任务继续正常工作并处理请求
下面的程序应该从多个线程定期打印,但是 tokio::time::sleep没有按我预期的那样工作: use tokio::prelude::*; //0.3.4 use tokio::runtime
我使用如下代码启动 Tokio 运行时: tokio::run(my_future); 我的 future 继续启动一堆任务以响应各种条件。 其中一项任务负责确定程序何时关闭。但是,我不知道如何让该任
我正在尝试构建一个可以管理来自 websocket 的提要但能够在多个提要之间切换的对象。 有一个 Feed 特征: trait Feed { async fn start(&mut self
我有一个设置,我的程序使用 std::thread::spawn 为 CPU 绑定(bind)计算生成多个线程。 我需要一个 GRPC 服务器来处理传入的命令并流式传输工作线程完成的输出。我正在为 G
我做计算机系统项目的第一个经历是使用 vanilla Java 构建服务器,然后在 Android 手机上构建客户端。从那时起,我发现有很多框架可以帮助管理可伸缩性并消除编写样板代码的需要。 我正在尝
我将从 Delphi XE4 迁移到 10.2。新的单位名称样式(深灰色背景上的黑色文本)不适合我的视力。有人可以建议如何更改它,最好不使用第 3 方加载项吗? 这就是新样式的样子,我很难阅读事件单位
我一直在寻找tokio源代码来获取问题的答案,并且给人以the sleep method literally puts a timer with duration的印象,但是我认为我可能误解了代码,因
我是一名优秀的程序员,十分优秀!