Skip to main content

Simple

In this example, we create a Piscina instance that uses a worker file to perform a simple addition operation. The main script (index.js) creates the Piscina instance and runs a task, while the worker script (worker.js) defines the task to be executed.

index.js
'use strict';

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

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

(async function () {
const result = await piscina.run({ a: 4, b: 6 });
console.log(result); // Prints 10
})();
worker.js
'use strict';

module.exports = ({ a, b }) => {
return a + b;
};

You can also check out this example on github.