New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide setup/bootstrap API for workers #136
Comments
One approach for this would be to pass in // main thread
const workerPool = new Piscina({ filename: '...', workerData: setupArgs }); // worker
const { workerData } = require('worker_threads');
async function init() {
// Do setup with workerData
return (task) => { /** worker handler **/ };
}
module.exports = init(); |
Hey @jasnell, thanks for reply! I think your suggested snippet would work for quite well for "single step setup" quite nicely (and also handle wokers created at later time already set up by default). My main ask was however about "multi step setup", so I could do parts of setup work in workers as soon as possible (arguments for those steps are not constants, they are being generated as pipeline move forwards. With single step I would have to wait with any setup until I have all needed pieces already available. The main problem with this for me is that setup/bootstrap steps in general are not "instant" or even reasonably quick (and annoyingly enough things that setup does ideally would be just shared, but they are just not serializable ... sigh) Silly illustration (that is approximation) of how my pipeline would look like with single setup step:
And here's how I see being able to exec setup in multiple steps (at least for The timing of each step is completely arbitrary in those charts - we don't have absolute control of it as we allow user's configuration and to extend things - so in some scenarios it's quite feasible that there would be no real benefits with multi step setup (however we do try to optimize for those "heavy"/demanding projects right now |
Closing this issue, as we decided that for our needs it will be just easier to roll our own solution. |
@pieh please also share your solution here along with some code so it will be easier for people who come across similar situation |
@rubiin Our solution just doesn't include use of In any case gatsbyjs/gatsby#32120 is currently worked on piece of code for anyone interested tho it is far from piscina as it has a bit different API, is not using |
As far as I can tell currently there is no builtin way/API to execute something on all created so far workers.
I have use case where tasks that should be distrubuted to workers require some setup ahead of time. Doing this setup on first actual task is not ideal because setup takes some time, so it would delay results. If I could trigger some setup function at arbitrary times that would execute on all existing workers this would mean that I can execute actual tasks right away - there is additional delay from when I could bootstrap workers to when I can distribute actual tasks so that delay could be nicely utilized so workers instead of being idle can already start preparing for tasks.
Some pseudo-code in main thread to illustrate it:
Without some API for this I do think it's possible using
BroadcastChannel
orMessagePort
per worker (not sure yet how, just noticed mention of this in #104) so I could message all workers to do some work + actual task handlers would need to ensure they would wait for that custom work to finish before they start executing, but this seems a bit messy or at least contrasting to otherwise nice API surface that is already exposed bypriscina
.What are your thoughts on supporting setups like that?
The text was updated successfully, but these errors were encountered: