Interleaving async calls into the same isolate/worker w/o deadlock
Closed this issue ยท 6 comments
Hey! Love rustyscript.
Not sure this is a feature request or a cry for help - here it goes.... imagine achieving the following w/o a deadlock:
- Rust calling async function in v8
- v8 calling Rust async function
- Above Rust async function calling back into v8.
Or a more concrete isomorphic problem statement: like https://github.com/rscarson/rustyscript/blob/master/examples/async_javascript.rs#L82 but poll p1, p2, p3 concurrently and respond to them in the order they finish rather than running the event loop to completion for p1, then p2, then p3. (If p1 was to depend on a call to rust, which in turn depends on an async js result, you'd have a deadlock)
I would like to build an event loop that interleaves calls coming from outside, putting them on the event loop, advancing the event loop, and then resolving outside calls for promises that happen to be resolved.
Ultimately, I'm always stumbling over the problem that I would have to borrow multiple &mut Runtime
. Even when I drop down to implementing my own event loop and using runtime.call_function_immediate
+ js_value::Promise
, I end up having to bind a a &mut Runtime
to test if the promise has already been resolved.
The only way I can think of right now, which I don't like very much, would be to break the async task chain on the rust side:
- Create a oneshot channel pair and a unique call context and return the receiver to the caller.
- Do a fire-and-forget
runtime.call_function_immediate
to a async js function that will return by calling itself into rust. - Keep advancing the event loop.
- When you get called by js, find the right sender for the call context and resolve it.
I would rather return values from js through Promises than stitching independent calls into and out of js. I hope I'm making sense. Is this something you've encounterd or thought about before?
Specifically, is there a way I could check whether a js_value::Promise
is pending or resolved w/o internally driving the event loop to completion?
Do you need the sandbox isolation? Or can you use some of the extension features?
Because if so I may have an idea on how to accomplish this
As for your question about promises I will add some more flexible resolution and query options
Do you need the sandbox isolation? Or can you use some of the extension features?
Because if so I may have an idea on how to accomplish this
I'm all ears.
As for your question about promises I will add some more flexible resolution and query options
Awesome. Does that include polling?
How would you feel about something like this:
https://gist.github.com/rscarson/f2d329a0e76609af7521bdf0185ad8a4
I think that matches the workflow you describe?
It starts a series of parallel async tasks, when when completed will trigger rust to call back into JS
Did it using the new broadcast_channel
feature
Awesome. Does that include polling?
It will now!
This was quick. Appreciate you! ๐
How would you feel about something like this:
https://gist.github.com/rscarson/f2d329a0e76609af7521bdf0185ad8a4
I played with your example, the event loop looks just like what I had in mind ๐
This was quick. Appreciate you! ๐
How would you feel about something like this:
https://gist.github.com/rscarson/f2d329a0e76609af7521bdf0185ad8a4I played with your example, the event loop looks just like what I had in mind ๐
Glad I could help!