Reqwest: Is this compatible with async-std?

Created on 26 Nov 2019  路  4Comments  路  Source: seanmonstar/reqwest

I was trying to use async_std::task::block_on with reqwest 0.10.0-alpha.2, and I got an error reqwest::Error { kind: Request, url: "xxxx", source: Error(Connect, Custom { kind: Other, error: "no current reactor" }) }

here's my code

use async_std::prelude::*;
// use async_std::io;
// use async_std::net;
use async_std::task::block_on;
use reqwest::Error;

async fn run() -> Result<(), Error> {
    let mut url = "https://www.google.com";
    let res = reqwest::get(url)
        .await?
        .text()
        .await?;

    println!("{}", &res);
    println!("hello");
    Ok(())
}


fn main(){
    match block_on(run()) {
        Ok(_) => (),
        Err(e) => println!("{:?}", e),
    }
}

How could I make this work?

Most helpful comment

There's some support for WASM, why not async-std too?

All 4 comments

I saw this on tokio site

Futures

Let鈥檚 take a closer look at futures. Tokio is built on top of the futures crate and uses its runtime model. This allows Tokio to interop with other libraries also using the futures crate.

So will reqwest possible use async-std block_on method?

No, as part of reqwest's goal of providing a powerful batteries-included HTTP client, it makes various decisions on dependencies. We choose to make use of the Tokio runtime.

The reason it won't work is because Tokio chooses a strategy that requires its event loop, IO driver, and timer to be available in the same thread as the executing future, instead of making those things be extra background threads with synchronization. That is why when for instance the TcpStream returns an error about "no current reactor" when used in a different runtime.

However, it's pretty simple to use reqwest with Tokio (reqwest 0.10.0-alpha.2 requires tokio 0.2.0-alpha.6, we're moving as quickly as possible to update to the final tokio 0.2.0 release):

#[tokio::main]
async fn main() -> Result<(), reqwest::Error> {
    let mut url = "https://www.google.com";
    let res = reqwest::get(url)
        .await?
        .text()
        .await?;

    println!("{}", &res);
    println!("hello");
    Ok(())
}

Tokio 2.0 already came out :), thank you for answering me

There's some support for WASM, why not async-std too?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

richardanaya picture richardanaya  路  5Comments

hwchen picture hwchen  路  4Comments

dbrgn picture dbrgn  路  5Comments

29e7e280-0d1c-4bba-98fe-f7cd3ca7500a picture 29e7e280-0d1c-4bba-98fe-f7cd3ca7500a  路  6Comments

encombhat picture encombhat  路  6Comments