在activx线程中执行同步http客户端获取



我有一个activx端点,我需要进行一个同步的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.

您应该尝试为此创建一个新线程:

std::thread::spawn(move || {
    reqwest::blocking::get(&url).unwrap().json().unwrap()
}).join().unwrap()

我不知道如何让它与reqwest一起工作(它肯定与activx有一些奇怪的冲突(,但出于某种原因,它与chttp一起工作得很好。

chttp::get(&fetch_url)?.text()?;

不能在async函数中使用阻塞函数。

使用reqwest::get().await代替reqwest::blocking::get()

最新更新