Replies: 1 comment 7 replies
-
We can accept a PR that adds ability to pass additional extensions to web workers.
That's most likely caused by the fact that you only initialize once and then clone. Should be solved if you can pass additional extensions to web worker and initialize them when passing. |
Beta Was this translation helpful? Give feedback.
-
I am embedding Deno within a Rust application to use as a JavaScript plugin engine and want to be able to send work to multiple Deno contexts (seperate threads) in a "round robin" fashion.
They don't need to be workers, they can be multiple independent instances of Deno.
To embed Deno I have vendored the
cli
folder into my project because the available Deno crates on crates.io are not sufficient to run a complete Deno instance with support for the Node.js standard library + Deno extras.I don't need a lot of the functionality in the deno_cli crate (like the lsp, test runner, benchmark runner, etc) but it's very challenging to lean out the crate due to the many interdependencies + making changes to the vendored deno_cli package will make syncing with deno updates quite difficult.
Due to conflicting dependencies (swc, amongst others) - I have had to silo Deno into a seperate
dylib
crate and consume it from my main application via Rust's FFI (Rust's FFI is unstable but in my case the Deno lib is compiled specifically for the consumer using the same settings).I have rewritten the entrypoint of Deno's CLI so that it exports the CLI commands as library entry points
becomes
reference
Initially, I simply tried to spawn multiple instances of Deno, each with its own channel which I use to send work to it.
Running multiple instances of Deno like this resulted in a segfault when more than one instance is launched (I assume this is because Deno CLI is using
static
globals for some internal state).So I figured, if multiple instances of Deno can't coexist, then I should try to spawn multiple worker threads and talk to them from the host process.
I am using my custom ops to communicate to/from the JavaScript context so I figured I could just access them from within a worker however the
create_web_worker_callback()
function in Deno CLI does not accept custom extensions.I tried to add custom extensions and run
custom_extensions.clone()
to pass them into the web worker, however this didn't work becauseExtension
cannot be cloned.So I tried to create a
Vec<Vec<Extension>>
where I would create multiple copies of my extensions (equivalent to the number of threads I will spawn) and each worker will grab their extensions withcustom_extensions.pop()
. This didn't work because something doesn't implementSend
, so I wrapped the extensions in anArc<Mutex<Vec<Vec<Extension>>>>
but this also didn't work.In the end, I modified the Deno CLI crate to hard code my extensions into the worker init options
This compiled and the ops were available in the workers - but obviously any changes to the vendored deno_cli package makes it harder to sync with Deno when there are updates.
However - this didn't work at runtime. Each worker calls my "hello world" extension which prints "hello world" - but it only prints once despite running on all workers.
My next experiment is to spawn multiple instances of the
deno_embed.so
dynamic library vialibloading
multiple times in the hope that I can create multiple independent Deno instances that don't share state.Any advice would be welcome
Beta Was this translation helpful? Give feedback.
All reactions