为什么我会收到错误 "there is no reactor running, must be called from the context of Tokio runtime",即使我有 #[tokio::main]?
Why do I get the error "there is no reactor running, must be called from the context of Tokio runtime" even though I have #[tokio::main]?
我按照 the mdns Rust documentation 并粘贴了示例代码,但它引发了以下错误:
thread 'main' panicked at 'there is no reactor running, must be called from the context of Tokio runtime'
这是我的代码:
use futures_util::{pin_mut, stream::StreamExt};
use mdns::{Error, Record, RecordKind};
use std::{net::IpAddr, time::Duration};
const SERVICE_NAME: &'static str = "_googlecast._tcp.local";
#[tokio::main]
async fn main() -> Result<(), Error> {
// Iterate through responses from each Cast device, asking for new devices every 15s
let stream = mdns::discover::all(SERVICE_NAME, Duration::from_secs(15))?.listen();
pin_mut!(stream);
while let Some(Ok(response)) = stream.next().await {
let addr = response.records().filter_map(self::to_ip_addr).next();
if let Some(addr) = addr {
println!("found cast device at {}", addr);
} else {
println!("cast device does not advertise address");
}
}
Ok(())
}
fn to_ip_addr(record: &Record) -> Option<IpAddr> {
match record.kind {
RecordKind::A(addr) => Some(addr.into()),
RecordKind::AAAA(addr) => Some(addr.into()),
_ => None,
}
}
依赖关系:
[dependencies]
mdns = "1.1.0"
futures-util = "0.3.8"
tokio = { version = "0.3.3", features = ["full"] }
我错过了什么?我尝试在线查找,但没有找到如何为这个用例创建反应器。
您正在使用较新版本的 Tokio,例如 0.3 或 1.x,许多软件包(包括 mdns 1.1.0)依赖于较旧版本的 Tokio,例如 0.2。
% cargo tree -d
tokio v0.2.22
└── mdns v1.1.0
└── example_project v0.1.0
tokio v0.3.3
└── example_project v0.1.0
目前,您需要匹配 Tokio 运行时的版本。最简单的方法是自己使用 Tokio 0.2。 tokio-compat-02 crate 在某些情况下也很有用。
另请参阅:
具有相同根本原因的各种错误消息:
there is no reactor running, must be called from the context of a Tokio 1.x runtime
there is no reactor running, must be called from the context of Tokio runtime
not currently running on the Tokio runtime
我的修复方法是将此添加到 Cargo.toml
:
[dependencies]
async-std = { version = "1", features = ["attributes", "tokio1"] }
在撰写本文时,相当数量的 crate 已经在使用 Tokio v1,但其他的可能仍处于实验阶段。检查您的包装箱中是否有 预发布版本,这些版本可能已经升级了它们的 tokio 运行时兼容性。
一个相关的例子是 actix-web
,它使用 Tokio 自第 4 版以来的运行时 1.0,仍处于测试阶段。
actix-web = { version = "4.0.0-beta.10" }
我按照 the mdns Rust documentation 并粘贴了示例代码,但它引发了以下错误:
thread 'main' panicked at 'there is no reactor running, must be called from the context of Tokio runtime'
这是我的代码:
use futures_util::{pin_mut, stream::StreamExt};
use mdns::{Error, Record, RecordKind};
use std::{net::IpAddr, time::Duration};
const SERVICE_NAME: &'static str = "_googlecast._tcp.local";
#[tokio::main]
async fn main() -> Result<(), Error> {
// Iterate through responses from each Cast device, asking for new devices every 15s
let stream = mdns::discover::all(SERVICE_NAME, Duration::from_secs(15))?.listen();
pin_mut!(stream);
while let Some(Ok(response)) = stream.next().await {
let addr = response.records().filter_map(self::to_ip_addr).next();
if let Some(addr) = addr {
println!("found cast device at {}", addr);
} else {
println!("cast device does not advertise address");
}
}
Ok(())
}
fn to_ip_addr(record: &Record) -> Option<IpAddr> {
match record.kind {
RecordKind::A(addr) => Some(addr.into()),
RecordKind::AAAA(addr) => Some(addr.into()),
_ => None,
}
}
依赖关系:
[dependencies]
mdns = "1.1.0"
futures-util = "0.3.8"
tokio = { version = "0.3.3", features = ["full"] }
我错过了什么?我尝试在线查找,但没有找到如何为这个用例创建反应器。
您正在使用较新版本的 Tokio,例如 0.3 或 1.x,许多软件包(包括 mdns 1.1.0)依赖于较旧版本的 Tokio,例如 0.2。
% cargo tree -d
tokio v0.2.22
└── mdns v1.1.0
└── example_project v0.1.0
tokio v0.3.3
└── example_project v0.1.0
目前,您需要匹配 Tokio 运行时的版本。最简单的方法是自己使用 Tokio 0.2。 tokio-compat-02 crate 在某些情况下也很有用。
另请参阅:
具有相同根本原因的各种错误消息:
there is no reactor running, must be called from the context of a Tokio 1.x runtime
there is no reactor running, must be called from the context of Tokio runtime
not currently running on the Tokio runtime
我的修复方法是将此添加到 Cargo.toml
:
[dependencies]
async-std = { version = "1", features = ["attributes", "tokio1"] }
在撰写本文时,相当数量的 crate 已经在使用 Tokio v1,但其他的可能仍处于实验阶段。检查您的包装箱中是否有 预发布版本,这些版本可能已经升级了它们的 tokio 运行时兼容性。
一个相关的例子是 actix-web
,它使用 Tokio 自第 4 版以来的运行时 1.0,仍处于测试阶段。
actix-web = { version = "4.0.0-beta.10" }