gpt4 book ai didi

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]?(为什么即使我有#[Tokio::Main],我也会收到错误“There is no Reector Running,Main Be Call from the Tokio Runtime”?)

转载 作者:bug小助手 更新时间:2023-10-25 16:00:11 35 4
gpt4 key购买 nike



I'm following the mdns Rust documentation and pasted the example code but it throws the following error:

我遵循了mdns Rust文档并粘贴了示例代码,但它抛出了以下错误:


thread 'main' panicked at 'there is no reactor running, must be called from the context of Tokio runtime'

Here's the code that I have:

以下是我拥有的代码:


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:

依赖关系:


[dependencies]
mdns = "1.1.0"
futures-util = "0.3.8"
tokio = { version = "0.3.3", features = ["full"] }

What am I missing? I tried looking online but haven't found how to create a reactor for this use case.

我遗漏了什么?我试着在网上寻找,但没有找到如何为这个用例创建一个反应堆。


更多回答
优秀答案推荐

You are using a newer version of Tokio, such as 0.3 or 1.x, and many packages, including mdns 1.1.0, rely on an older version of Tokio, such as 0.2.

您使用的是较新版本的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

For now, you will need to match versions of the Tokio runtime. The easiest way is to use Tokio 0.2 yourself. The tokio-compat-02 crate may also be useful in some cases.

目前,您需要匹配Tokio运行时的版本。最简单的方法是自己使用Tokio 0.2。Tokio-Compat-02板条箱在某些情况下可能也很有用。


See also:

另见:





Various error messages with the same root cause:

具有相同根本原因的各种错误消息:



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




Fix for me was adding this to Cargo.toml:

我的解决办法是将这一点添加到Cargo.toml:


[dependencies]
async-std = { version = "1", features = ["attributes", "tokio1"] }

https://github.com/ATiltedTree/ytextract/issues/25

Https://github.com/ATiltedTree/ytextract/issues/25



At the time of writing, a fair amount of crates are already using Tokio v1, but others might still be under an experimental phase. Check your crates for prerelease versions which might have already upgraded their tokio runtime compatibility.

在撰写本文时,相当数量的板条箱已经在使用Tokio v1,但其他板条箱可能仍处于实验阶段。检查您的板条箱中可能已经升级了Tokio运行时兼容性的预发布版本。


A relevant example of this was actix-web, which uses runtime 1.0 of Tokio since version 4. Although there were prereleases of this major increment since 2022-01-07, version 4.0.0 was only released in 2022-02-25.

一个相关的例子是Actix-web,它从版本4开始使用Tokio的运行时1.0。虽然从2022-01-07开始就有了这一主要增量的预发布,但版本4.0.0是在2022-02-25才发布的。


actix-web = { version = "4.0.0-beta.10" }


I ran the same code that produced the error with only changes to the dependencies.

我运行的代码与生成错误的代码相同,只是更改了依赖项。


[dependencies]
mdns = "3.0.0"
futures-util = "0.3.28"
tokio = { version = "1.32.0", features
= ["full"] }

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,
}
}

// A small sample of the output I
// received without error is below:

found cast device at 10.0.0.165
found cast device at 10.0.0.171
found cast device at 10.0.0.158


更多回答

Thanks. I had this error exactly in actix-web and was about to dissappointingly downgrade my tokio to 0.2. I prefer less stable beta over too old <1 versions. :P

谢谢。我正好在Actix-Web上犯了这个错误,并即将令人失望地将我的Tokio降级到0.2。我更喜欢不太稳定的测试版,而不是太老的<1版本。:p

This answer needs to provide more supporting information and something new/unique that the other answers don't provide.

这个答案需要提供更多的支持信息和其他答案没有提供的新的/独一无二的东西。

The following code is the same as the person receiving the following error:

以下代码与收到以下错误的人相同:

I added more substance to my original submittal. Hope this helps.

我在我最初提交的材料中增加了更多的内容。希望这能帮上忙。

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com