rust - 在actix线程中进行同步http客户端访存

标签 rust rust-actix actix-web reqwest

我有一个actix端点,我需要执行一个同步的http客户端访存来获取一些结果,并返回一些数据。我的端点不能使用async,所以我不能使用任何.await方法。

我尝试在端点中使用reqwests阻止客户端,如下所示:

{ ...

  let res = reqwest::blocking::get(&fetch_url)?
    .json::<MyResp>()?;
  ...

但这给了我错误:
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.', /.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.9/src/runtime/enter.rs:19:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

最佳答案

我不知道如何使它与reqwest一起使用(它必须与actix有一些怪异的冲突),但是由于某种原因,它与chttp可以很好地工作。
chttp::get(&fetch_url)?.text()?;

关于rust - 在actix线程中进行同步http客户端访存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60580769/

相关文章:

rust - Actix TCP客户端实现的编译问题

Rust actix-web : the trait `Handler<_, _>` is not implemented

arrays - 将已知的编译时大小切片到数组,绝对正确

rust - 我在这段代码中为队列实现 `dequeue` 函数做错了什么?

rust - 我如何存储闭包并将它们与 Actix actors 一起使用?

rust - 如何读取 HttpRequest 正文

rust - 在 FromRequest 实现中获取应用程序状态

rust - "Urlencoded payload size is bigger than allowed (default: 256kB)"

rust - 在继承自另一个特征的特征中指定关联类型

rust - 以索引为键的 serde_json 展平对象