worker-process
Dead simple worker child processes for Node.js with a modern Promise API and a binary IPC.
For people who don't want to use external servers for binary IPC,
don't want to mess with child_process
, and want to use something
that "just works".
master.js
{ // Create a worker. const worker = await let gotMessage = false await Promise // If the worker exits successfully but you didn't finish what you wanted to // do, you can handle it if !gotMessage throw 'Worker did not say hello :(' }
worker.js
{ // Connect to the IPC socket. // All nuances and errors from here on will be handled by worker-process // by logging the error and exiting the process with exit code 1 to // guarantee safety and consistency const workerConnection = await // Handle messages from the parent process workerConnection { console } // Send messages to the parent process const data = 'hello' 'world!' await workerConnection // Wait for the message to complete sending, and then close the connection await workerConnection} console
I recommend using MessagePack as the IPC message format.
How it works
The "worker" process is created via child_process.spawn()
. At spawn, a fourth
stdio is created (fd = 3
), which is a node.js duplex stream because node.js
creates a duplex socket, rather
than a pipe for communication. When the child process is connected, connectivity
is assured by sending the handshake message, 0x00
, and waiting for the worker
process to send 0x01
back. From there, data is passed back and forth using the
binary protocol.