4 ms·
Honest question. Are we going to be able to pass functions with data to threads? Or are these only designed to use files? Which makes this feel like a better wr
by Trisell 8y ago
Honest question. Are we going to be able to pass functions with data to threads? Or are these only designed to use files? Which makes this feel like a better wrapper on the child process api that already exists.
- ryanpetrich 8y agoThe code to run in the worker is passed as a string or a path, so no data can be captured. Data can be sent to the worker by posting messages, but the data is never shared—only copied. A limited set of types can be transferred to a worker, after which point they will become unusable in the parent.
- 11235813213455 8y agoFor sharing (byte) data, I think there is SharedArrayBuffer https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... But else, yes, the sent and received data is serialized (when using ipc, else you can also send raw data through streams, and handle the serialization yourself)
- primq 8y agomemory sharing is explained in the article.
- rndgermandude 8y agoSharedArrayBuffer -> shared ArrayBuffer -> ownership can be transferred around, but the data can only be owned by one thread at a time. The data itself isn't copied. Passing around ArrayBuffers back and forth is good enough for a lot of stuff. In a worker you can then put a node Buffer, TypedArray, or DataView around the ArrayBuffer again, if you want.
- ilaksh 8y agoSupposedly SharedArrayBuffer and atomics have been in Node.js since version 9. I wonder if there are any articles where people use them in threads.
- snek 8y agoThere has been some light discussion in the standards bodies about this, and so far the write-up looks like this: https://github.com/domenic/proposal-blocks https://github.com/domenic/proposal-blocks