Embed Deno into Rust App: How to call other method with poll_event_loop keep going
Hi, all, I'm trying embed deno_core into Rust Gateway. like openresty(lua) in NGINX but for Deno(javascript)
After reading the docs, I need continuously call https://docs.rs/deno_core/latest/deno_core/struct.JsRuntime.html#method.run_event_loop to make deno event loop running. Sometime, after gateway received request headers, we need to call method
on_recv_request_header
in Deno script, but due to the call to run_event_loop, we already await blocked in there, I can't get JsRuntime back, So I need find a way to stop run_event_loop temporarily, get JsRuntime back , then call run_event_loop again
Possible solution:
It there have a method so I can get JsRuntime handle(like https://docs.rs/tokio/latest/tokio/runtime/struct.Runtime.html#method.handle), move the run_event_loop to another async task, then call other method in current task?Runtime in tokio::runtime - Rust
The Tokio runtime.
JsRuntime in deno_core - Rust
A single execution context of JavaScript. Corresponds roughly to the “Web Worker” concept in the DOM.
10 Replies
Deno
Roll your own JavaScript runtime | Deno
A walk-through of creating a CLI that executes JavaScript files.
https://github.com/rscarson/rustyscript
This might be helpful, it's exactly kind of use case I wrote it for. And if not my documentation and code should give you some ideas
I know this, but I don't know how to handle the situation where I call async js function from rust, waiting the promise result in rust, at the same time, the eventloop should continue to run since there have many async task in JavaScript side.
the post just call run_eventloop to end, i can't get js runtime to do other operation once call run_eventloop.await
Hi, thank you. but the rustyscript also run js to end, and then the control come back to rust. I want to call async js callback when http request coming in rust side. everytime http.request coming from rust side, I need to call async js callback, wait the result, also poll event loop because settimeout need to trigger in js side. many requests, many calls to js, many futures to resolve, plus eventloop
You should be able to do all that with the built-in stuff rustyscript includes
Actually I think one of the examples I have is almost verbatim that
If you use my promise type I think it gets you 90% of the way there if I'm understanding you correctly
does there have code example?
There does!
https://rscarson.github.io/rustyscript-book/
I will find you the specific sections tomorrow because it is late and my brain is mashed potatoes. But it's in the rusty script book, the async section is what you're after
Let's say we have a HTTP/2 connection which spawn many requests at the same time.
@boopus_the_snootus
Immediate
is what I wanted(get promise but resolve later by move to another tokio task), https://rscarson.github.io/rustyscript-book/advanced/asynchronous_javascript.html#_immediate
But the Examples shows that async block future
still need mutably borrow js runtime, future
cannot spread to another tokio::task::spawn_local
which requires 'static
, restrict the usage in single thread concurrent environment.
Also, I don't want block_on
current thread until future resolved.GitHub
rustyscript/examples/async_javascript.rs at master · rscarson/rust...
Effortless JS integration for rust . Contribute to rscarson/rustyscript development by creating an account on GitHub.
Something like this?
https://github.com/rscarson/rustyscript/blob/master/examples%2Fbackground_tasks.rs
not quite, there no concurrent task(
tokio::task::spawn_local
) spawned.