-
Notifications
You must be signed in to change notification settings - Fork 285
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
Sending class instances to worker threads #1558
Comments
$ cat 1558.js const {
Worker, isMainThread, parentPort, workerData
} = require('worker_threads');
class Person {
constructor(name, surname) {
this.name = name
this.surname = surname
}
fullname() {
return `${this.name} ${this.surname}`
}
static whoAmI() {
return 'a Person'
}
}
if (isMainThread) {
const foo = new Person('foo', 'bar')
const worker = new Worker(__filename, {});
worker.on('message', (m) => {
console.log(`worker message: ${JSON.stringify(m)}`)
})
worker.postMessage(foo) // foo is serialized
} else {
parentPort.on('message', foo => {
console.log(`in worker, foo is like: ${JSON.stringify(foo)}`)
parentPort.postMessage(foo)
})
} $ node --experimental-worker 1558
^C I believe the member functions are omitted upon serialization because the serializer defines a data-only specification. However, copying @addaleax to confirm. |
@gireeshpunathil That example is different. |
just shift the Serialization exists, and is possible through the way I described. If you doubt, please split the main and worker logic into 2 files and check; it works. The only caveat is with member methods, which is inhibited by the JSON spec, not by any worker limitation. Hope this helps. |
Sorry, I didn't explain myself well. Finally, yes, you're just considering the props of the instance (and they do work with JSON.stringify), not the methods. To better understand what I'm saying, take a look to GoLang coroutines: they share the scope and that's basically the same idea I'm trying to reproduce. |
I think the issue here is that If you have that class available in both the sender and the receiver environment, and you know how the data is structured, you can probably address this through some post-processing – the main difference between a class instance and a “normal” object are their prototypes, so by setting the prototype of the received object to |
ping @wilk |
inactive, closing |
@gireeshpunathil Sorry, I forgot this thread. |
ok, so - is your requirement something like defineClass in Java? that takes raw bytes as input and defines a Class from those? |
ping @wilk |
Coming from Go, I'm also missing an elegant way of using complex objects/instances in shared memory. I'm working on a prepress engine, that uses multiple functional 'nodes' for image processing etc. where the nodes (executed inside a worker thread) need access to more complex global objects (web servers, etc.) from the parent application. |
@gireeshpunathil I'm not sure. Consider the following example:
What I need is to pass the instance
Then also the passed
|
js only has one thread safe data structure, SharedArrayBuffer. everything else must be copied from scratch. |
So … to expand on what I’ve said above:
|
inactive, and / or resolved, closing. pls feel free to reopen if required |
I'm trying to figure out how can I pass complex structures, like class instances, to worker threads.
I tried using strings and just postMessage but well there's no an easy-to-use way to serialize/deserialize class instances.
I read about SharedArrayBuffer but I don't know how to convert a class instance in pure bytes.
How can I do that?
Example:
The text was updated successfully, but these errors were encountered: