Exidex
Exidex13mo ago

How do I enqueue_microtask from other thread

so Isolate.enqueue_microtask is obviously not send/sync so I think the only option is to use std::sync::mpsc::channel and use Receiver.try_recv to get the values from Sender on other thread. The problem is that I am being a rust newbie don't know how to combine them on the single thread or if it actually possible. I somehow need to wake up the runtime when the Sender actually sends the value, I think I can wrap try_recv with poll_fn and use something like tokio's select! macro but that also doesn't seem to fit my problem because I need to be able to restart run_event_loop to process any new microtasks. Any pointers?
5 Replies
Andreu Botella (they/them)
the easiest way is probably to have an op that listens for mpsc messages the op would resolve when the message arrives, and return the passed value to JS and then on an extension script, you would call that op on an async loop
(async () => {
while (true) {
const value = await Deno.core.opAsync("op_mspc_recv");
process(value);
}
})();
(async () => {
while (true) {
const value = await Deno.core.opAsync("op_mspc_recv");
process(value);
}
})();
Exidex
Exidex13mo ago
cool, that looks like it could actually work. thanks for the help
Andreu Botella (they/them)
that loop would keep the event loop alive if you don't want that, you can do
const promise = Deno.core.opAsync("op_mspc_revc");
Deno.core.unrefOp(promise[Symbol.for("Deno.core.internalPromiseId")]);
const value = await promise;
const promise = Deno.core.opAsync("op_mspc_revc");
Deno.core.unrefOp(promise[Symbol.for("Deno.core.internalPromiseId")]);
const value = await promise;
similar to how Deno.unrefTimer works
Exidex
Exidex13mo ago
how is it better/worse? oh it doesn't block the event loop from stopping