gpt4 book ai didi

rust - 即使我有#[tokio::main],为什么仍然出现错误 “there is no reactor running, must be called from the context of Tokio runtime”?

转载 作者:行者123 更新时间:2023-12-03 11:23:20 27 4
gpt4 key购买 nike

我遵循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"] }
我想念什么?我尝试在网上查找,但没有找到如何为该用例创建 react 堆。

最佳答案

您使用的是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 在某些情况下也可能有用。
也可以看看:
  • Why is a trait not implemented for a type that clearly has it implemented?

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

    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

    关于rust - 即使我有#[tokio::main],为什么仍然出现错误 “there is no reactor running, must be called from the context of Tokio runtime”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64779920/

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