(Newb) support for node worker threads?

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.

2 Likes

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?)

1 Like

Small note, ReScript supports the equivalent __FILE__ macro directly, so no need for a binding.

2 Likes