Giter VIP home page Giter VIP logo

Comments (5)

notgull avatar notgull commented on June 10, 2024

You can fit hyper into smol, see this example (this uses smol::spawn but it shouldn't be too hard to replace that with a real executor).

You can also use async-h1, but it imports async-std.

from async-task.

maxwellflitton avatar maxwellflitton commented on June 10, 2024

thanks for the response. I'm still getting a Tokio error. I appreciate I might be doing something stupid but I've created an executor like the following:

#[derive(Clone)]
struct SmolExecutor;

impl<F: Future + Send + 'static> hyper::rt::Executor<F> for SmolExecutor {
    fn execute(&self, fut: F) {
        spawn_task!(async { drop(fut.await) }).detach();
    }
}

The spawn_task! macro is a wrap around for the following function:

fn spawn_task<F, T>(future: F, order: FutureType) -> Task<T>
where
    F: Future<Output = T> + Send + 'static,
    T: Send + 'static,
{
    static HIGH_CHANNEL: Lazy<(Sender<Runnable>, Receiver<Runnable>)> = Lazy::new(|| 
        {flume::unbounded::<Runnable>()}
    );
    static LOW_CHANNEL: Lazy<(Sender<Runnable>, Receiver<Runnable>)> = Lazy::new(|| 
        {flume::unbounded::<Runnable>()}
    );
    static HIGH_QUEUE: Lazy<flume::Sender<Runnable>> = Lazy::new(|| {
. . .

Which goes onto spawn tasks and send runnables to the queue. The spawn_task! macro works fine when called directly for async functions and structs that have implemented the Future trait. However, with the following hyper code I try and make a simple get http request:

use hyper::{Client, Uri};
. . .
    let future  = async {
        let connector = hyper::client::HttpConnector::new();
        let client = Client::builder().executor(SmolExecutor).build::<_, hyper::Body>(connector);
        let url = "http://httpbin.org/get".parse::<Uri>().unwrap();
        let response = client.get(url).await.unwrap();
    
        // Print the response body
        let body = hyper::body::to_bytes(response.into_body()).await.unwrap();
        println!("Response body: {:?}", body);
    };

    let test = spawn_task!(future);
    let _outcome = future::block_on(test);

And I get the following error:

thread '<unnamed>' panicked at 'there is no reactor running, must be called from the context of a Tokio 1.x runtime'

I'm currently using the following hyper crate:

hyper = { version = "0.14.26", features = ["http1", "http2", "client", "runtime"] }

Is there something obvious I'm missing?

from async-task.

notgull avatar notgull commented on June 10, 2024

You're using the HttpConnector type, which uses tokio under the hood... and since you're not using the tokio runtime, it causes that panic.

What you want to do is define your own connector type using smol primitives. See here for an example of how this may be done.

from async-task.

maxwellflitton avatar maxwellflitton commented on June 10, 2024

thank you for pointing me in the right direction. Everything is working now as it should. I will talk to the editors and see if I can dedicate two chapters to smol as I have to guide the readers through the code and casually lumping in all the client code at the end of chapter three would not be good for readers trying to learn about async. However, not including how to implement something like HTTP into the runtime they spent a whole chapter doing would also not be a good experience. Also sorry about not checking the examples before posting here. I will check out the examples before bothering you again. really appreciate the quick feedback

from async-task.

notgull avatar notgull commented on June 10, 2024

No problem! Let me know if you have any other questions, I'm happy to answer them.

Closing this issue for now since I think it's been answered to satisfaction.

from async-task.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.