-
Notifications
You must be signed in to change notification settings - Fork 123
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
Add support for worker_threads #90
base: master
Are you sure you want to change the base?
Conversation
Sweet! You'll have to be patient with me, I'm a bit busy this week but I'd love to get this landed and released. Feel free to ping me after this week if you don't hear from me again (overloaded with input so things tend to get forgotten). Initial look at the code is 👍 though. |
Thanks, I'll keep that in mind. In the meantime I've come to think that it should also be possible to pass in a transferList to efficiently pass data around between the worker thread and the main thread. The most obvious approach would be to do something like this: // In the worker
module.exports = function(cb) {
let arr = new Float32Array([Math.E]);
let transferList = [arr.buffer];
cb(null, arr, transferList);
}; The thing is that this would break the existing api, as currently in handle.js, all arguments passed in the callback are sent to the parent: let _args = Array.prototype.slice.call(arguments);
send({ idx: idx, child: child, args: _args }); I think this will need to be changed to something like let _args = Array.prototype.slice.call(arguments);
if (worker_threads) {
send({ idx: idx, child: child, args: _args.slice(0,2) }, _args[2]);
}
else {
send({ idx: idx, child: child, args: _args });
} but this causes an api difference between worker_threads and the default child_process implementation. @rvagg What's your opinion on this? |
lib/fork.js
Outdated
function fork (forkModule, workerOptions, worker_threads) { | ||
|
||
// Check whether we need to run using worker_threads. | ||
if (worker_threads) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I get why you've gone with this name but I think I'd prefer it be workerThreads
lib/fork.js
Outdated
checked = true | ||
} | ||
|
||
if (available) return thread(forkModule, workerOptions); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
break a newline here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and swallow the blank line underneath
|
I will see if I find some time this week to modify the code a bit to implement the TransferList & adapt the readme. |
When using the module using threads, it's now possible to specify a transferList as thrid argument to the callback. As a consequence there's a breaking change so that when using the child_process version, the third argument to the callback is ignored and no longer sent to the parent process.
I've added the transferList functionality and modified the readme to document it. I did have to comment out some process.id related stuff in the tests though because they wouldn't pass otherwise. This is probably related to my Windows machine. Notably the t.ok(pid > process.pid, 'pid makes sense')
t.ok(pid < process.pid + 750, 'pid makes sense') tests did not pass. |
Yeah, that's OK on the PID tests, they're pretty specific to a particular implementation of PIDs that doesn't hold universally, even on Linuxes. |
One more thing: I didn't update the index.d.ts file because I don't know TypeScript, but it probably should be updated as well before shipping this. |
maybe @zeroEvidence can help us out on the typescript definitions for these changes |
Hey guys! That's a really nice feature, I'd like it to be released as soon as possible. Can I help somehow with type definitions? |
Pardon for the delay, I've been sick. I can give it a shot. @rvagg May I recommend that you consider merging this to a dev branch so I can grab the changes from the original repo? |
@zeroEvidence That would be great! Give me a shout if you need some help with figuring out the new api. The main difference is the addition of the callback(err, result, transferList); and when passing a transferList to the worker you specify the transferList after the callback worker(...arguments, callback, transferList); |
Given that node 12 - which doesn't require a flag for worker threads - entered LTS two days ago, I was wondering whether we shouldn't make the threaded implementation the default when we release it. @rvagg, what is your opinion on this? |
Any updates on this pull request ? |
hey @rvagg how are we feeling about this PR? I'd been looking to replace worker-farm with node workers but the main stumbling block is that the load balancing logic is pretty handy (Might possibly be handy as a separate module) and I came to ask the question that @sebamarynissen has already answered with this PR (what if we change worker-farm instead) @sebamarynissen how have you been working around this PR getting stuck? |
I use this in a production website of mine where I simply referred to my fork when specifying it as a dependency, like {
"dependencies": {
"worker-farm": "git+https://github.com/sebamarynissen/node-worker-farm.git#feature/esm"
}
} Note that the branch I'm referring to is a more recent version where I refactored the code a bit more to my taste (using promises instead of callbacks), and also added the possibility to use es modules as the worker, given that currently only commonjs is supported. I never created a new PR for it though because it contained quite a few breaking changes. |
Hey @sebamarynissen, are you interested in, and can I trust you with, commit access to this project? I just don't use it and don't have the time to give it the care that it needs and there's a lot of people who still want to use it and want it in a better condition. Ideally we could find a few more people to collaborate and review each others' work too. |
@sebamarynissen Those would have made a good 2.0 Perhaps you should call this one 1.8 since it has one breaking change? |
@rvagg, yeah I'm not sure. The project for which I initially made this PR got cancelled, and the production website I use it in is a small side project that I haven't looked into anymore for quite some time now. On top of that, I'm trying to start/run a business next to my main job, so I too feel that I cannot give the project the care that it needs. |
Since node 10.5 we have the option of using worker threads which have the advantage that they can use shared memory using
SharedArrayBuffer
s. I've modified the worker-farm module to optionally support worker_threads. The api is completely the same and can be used like:The worker_threads module is still experimental and should be explicitly enabled using the
--experimental-worker
flag. This is automatically detected, and ifworker_threads
is not available, it falls back to the default forked implementation, showing a warning thatworker_threads
is not available.