Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is this compatible with async-std? #719

Closed
CGQAQ opened this issue Nov 26, 2019 · 4 comments
Closed

Is this compatible with async-std? #719

CGQAQ opened this issue Nov 26, 2019 · 4 comments

Comments

@CGQAQ
Copy link

CGQAQ commented Nov 26, 2019

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?

@CGQAQ
Copy link
Author

CGQAQ commented Nov 26, 2019

I saw this on tokio site

Futures

Let’s 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?

@seanmonstar
Copy link
Owner

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(())
}

@CGQAQ
Copy link
Author

CGQAQ commented Nov 26, 2019

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

@kellpossible
Copy link

kellpossible commented Sep 7, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants