You can implement a [worker_threads](https://nodejs.org/api/worker_threads.html#class-worker) worker in a simple way by extending the class _ThreadWorker_:
```js
-'use strict'
-const { ThreadWorker } = require('poolifier')
+import { ThreadWorker } from 'poolifier'
function yourFunction(data) {
// this will be executed in the worker thread,
Instantiate your pool based on your needs :
```js
-'use strict'
-const { DynamicThreadPool, FixedThreadPool, PoolEvents, availableParallelism } = require('poolifier')
+import { DynamicThreadPool, FixedThreadPool, PoolEvents, availableParallelism } from 'poolifier'
// a fixed worker_threads pool
const pool = new FixedThreadPool(availableParallelism(), './yourWorker.js', {
- errorHandler: (e) => console.error(e),
+ errorHandler: e => console.error(e),
onlineHandler: () => console.info('worker is online')
})
// or a dynamic worker_threads pool
const pool = new DynamicThreadPool(Math.floor(availableParallelism() / 2), availableParallelism(), './yourWorker.js', {
- errorHandler: (e) => console.error(e),
+ errorHandler: e => console.error(e),
onlineHandler: () => console.info('worker is online')
})
// so you can easily switch from one to another
pool
.execute()
- .then((res) => {
+ .then(res => {
console.info(res)
})
- .catch((err) => {
+ .catch(err => {
console.error(err)
})
```