Skip to main content

Messages

Piscina allows workers to send messages back to the main thread using the parentPort.postMessage() method. This can be useful for sending progress updates, or intermediate results during the execution of a long-running task.

index.js
'use strict';

const Piscina = require('piscina');
const { resolve } = require('path');

const piscina = new Piscina({
filename: resolve(__dirname, 'worker.js')
});

(async function () {
piscina.on('message', (event) => {
console.log('Message received from worker: ', event);
});

await piscina.run();
})();
worker.js
'use strict';
const { parentPort } = require('worker_threads');

module.exports = () => {
parentPort.postMessage('hello from the worker pool');
};

You can also check out this example on github.