-
Notifications
You must be signed in to change notification settings - Fork 6
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
Worker Threads: "Module did not self-register" #10
Comments
I just tested it with Version
const { Worker } = require('worker_threads');
const worker = new Worker('./worker.js', { workerData: 'yo' });
worker.on('message', console.log);
worker.on('error', console.error);
worker.on('exit', code => console.log('Worker exit: ', code));
const { workerData, parentPort } = require('worker_threads');
const lzo = require('.');
parentPort.postMessage(lzo.compress(workerData)); Result
|
However, I found this Stackoverflow answer and in my |
I’d be happy to test a new version. Glad to see there is nothing fundamentally wrong. |
I will aim to do that later today. |
I just published a new version. For some reason it's not yet visible on the npm website, but I can see it in the registry json. |
I have been able to get a simple test case working with 0.4.7 that uses lzo in a worker thread. However, there's some other complexity in my app that is still leading to the same result. I'll let you know when I track it down. Thanks for the help. |
Ok, it turns out that this issue happens when I require the lzo module in both the parent and the worker. Here is my test code: parent.js: 'use strict';
const util = require('util');
const { Worker } = require('worker_threads');
function sendMessage(worker, operation, input) {
return new Promise((resolve, reject) => {
// Process response message
worker.on('message', response => {
console.log(`Parent: received ${util.inspect(response)}`);
if (response.errorMessage) {
return reject(new Error(response.errorMessage));
}
return resolve(response.result);
});
// Send message to worker
const message = {
operation,
input,
};
console.log(`Parent: sending ${util.inspect(message)}`);
worker.postMessage(message);
});
}
//const lzo = require('lzo'); // uncommenting this line causes the issue
const worker = new Worker('./child.js');
const input = 'foobar';
sendMessage(worker, 'compress', input).then(compressed => {
console.log(`Parent: got compressed result from worker: ${compressed}`);
sendMessage(worker, 'decompress', compressed).then(decompressed => {
console.log(`Parent: got decompressed result from worker: ${Buffer.from(decompressed).toString()}`);
process.exit();
});
}).catch(err => {
console.error(`Parent: got error from worker: ${result}`);
process.exit(1);
}); child.js: 'use strict';
const lzo = require('lzo');
const util = require('util');
const { parentPort, workerData } = require('worker_threads');
function compress(message) {
return lzo.compress(message);
}
function decompress(message) {
return lzo.decompress(message, message.length * 10);
}
function sendResponse(operation, result) {
const response = {
operation,
result,
};
console.log(`Child: sending ${util.inspect(response)}`);
parentPort.postMessage(response);
}
function sendError(operation, err) {
const response = {
operation,
errorMessage: err.message, // can't serialize Errors
};
console.log(`Child: sending ${util.inspect(response)}`);
parentPort.postMessage(response);
}
parentPort.on('message', message => {
console.log(`Child: received ${util.inspect(message)}`);
let result;
switch(message.operation) {
case 'compress':
result = compress(message.input);
sendResponse(message.operation, result);
break;
case 'decompress':
result = decompress(message.input);
sendResponse(message.operation, result);
break;
case 'terminate':
process.exit(0);
break;
}
}); Uncommenting the require of lzo in parent.js causes the error, which occurs in the worker thread during the second require.
|
I don't know if this applies to you, but this might help? nodejs/node#21783 (comment) |
That definitely looks like it might help, I will check it out and try to publish a new release next week. Thanks for your research so far :) |
This is published in Version |
Thank you! |
When using the new experimental worker threads in Node 10.5 and above, if you require lzo in the worker, the following error is thrown:
This seems to be related to nodejs/node#21783 and nodejs/node#21481. From the comments, it appears that this is a special case that authors of native addons need to handle. I'm really keen on getting this to work. Let me know if it's already on your radar or if you'd like some help with it. Thanks!
The text was updated successfully, but these errors were encountered: