Comments (5)
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.
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.
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.
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.
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)
- Option to avoid panicking when polling a cancelled task HOT 3
- Task not get rescheduled randomly HOT 1
- Can there be no additional scheduling of tasks when Task cancel or Detach HOT 1
- Custom allocators HOT 3
- How can I clone a `Task` HOT 6
- Add names to tasks HOT 1
- Dummy task HOT 2
- Bug: Unit test with expected panic message randomly fails because of crate-specific panic message HOT 2
- Why was metadata removed from a previous version of async-task? HOT 2
- Run tests for async-executor in CI
- Efficiently support bounded queues for scheduling? HOT 5
- Why manual call of schedule is needed on runnable? HOT 2
- fails to test examples without feature std HOT 1
- Use portable-atomic HOT 3
- Unclear behaviour of casting ptr of RawTask to ptr of Header HOT 1
- When does schedule called? HOT 4
- Implicit boxing of large futures causes excessive monomorphization HOT 2
- Clarify safety of spawn_unchecked for Runnable lifetime HOT 2
- `Builder::spawn` should encode the metadata lifetime contract HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from async-task.