Title says it all really :-).
Is there a reason not to be using rescript as a general purpose language targeted to node?
Thanks!
Title says it all really :-).
Is there a reason not to be using rescript as a general purpose language targeted to node?
Thanks!
ReScript is a general purpose language like typescript. Do you have any problem targeting node?
Great to hear - thanks! Iām just looking for examples of using a worker thread with rescript, but I canāt find any bindings or examples of sending or sharing immutable rescript data structures to a worker
Once you have some familiarity with bindings they are not too difficult to write.
Hereās some example JS code:
const { Worker, isMainThread, parentPort } = require("worker_threads");
if (isMainThread) {
const worker = new Worker(__filename);
worker.on("message", (msg) => {
console.log(msg);
});
} else {
parentPort.postMessage("Hello world!");
}
And one way to write it in ReScript:
module Global = {
@val
external __filename: string = "__filename"
}
module WorkerThreads = {
module Worker = {
type t
type sendCallback = string => unit
@send
external on: (t, string, sendCallback) => unit = "on"
}
module ParentPort = {
type t
@send
external postMessage: (t, string) => unit = "postMessage"
}
@module("worker_threads") @value
external isMainThread: bool = "isMainThread"
@module("worker_threads") @value
external parentPort: ParentPort.t = "parentPort"
@module("worker_threads") @new
external makeWorker: string => Worker.t = "Worker"
}
if WorkerThreads.isMainThread {
let worker = WorkerThreads.makeWorker(Global.__filename)
worker->WorkerThreads.Worker.on("message", msg => {
Js.log(msg)
})
} else {
WorkerThreads.parentPort->WorkerThreads.ParentPort.postMessage("Hello world!")
}
Just posting this as an example that might help you get started.
Many thanks - that is really helpful.
Is it possible to take advantage of āimmutable data structuresā so we arenāt serialising/deserialising every time we send/receive data from the worker, or is that just a limitation of worker threads?
Hi @yatesco, I donāt have much experience with Node worker threads, but my understanding is that data transfers use cloned data, which avoids problems of mutation? However I might be misunderstanding what you have in mind. Otherwise perhaps you have some example JS code that shows what you are trying to achieve?
Thanks @kevanstannard. Iām just at the āthinkingā stage at the moment, but it seems unfortunate from a performance POV that data must be cloned given the use of immutable data structures which are safe to share in memory.
Iām only considering workers because some of the stuff I need to do is pretty CPU intensive. As ever, I need to benchmark all of this rather than just guessing :-).
Thanks again Kevan (did I get that right?)
Small note, ReScript supports the equivalent __FILE__
macro directly, so no need for a binding.