Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
node-version: [18.x, 20.x, 22.x]
node-version: [20.x, 22.x]
runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v4
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
node_modules
dist
coverage
tsconfig.tsbuildinfo
2 changes: 1 addition & 1 deletion .taprc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs: 2
test-env: TS_NODE_PROJECT=test/tsconfig.json
test-ignore: $.
test-regex: ((\/|^)(test?|__test?__)\/.*|\.(tests?|spec)|^\/?tests?)\.([mc]js|ts)$
timeout: 60
timeout: 10
ts: true
jsx: false
flow: false
93 changes: 47 additions & 46 deletions docs/docs/api-reference/class.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,55 +55,54 @@ This class extends [`EventEmitter`](https://nodejs.org/api/events.html) from Nod
returning control to the main thread (avoid having open handles within a thread). If still want to have the possibility
of having open handles or handle asynchrnous tasks, you can set the environment variable `PISCINA_ENABLE_ASYNC_ATOMICS` to `1` or setting `options.atomics` to `async`.

** :::info
:::info
**Note**: The `async` mode comes with performance penalties and can lead to undesired behaviour if open handles are not tracked correctly.
Workers should be designed to wait for all operations to finish before returning control to the main thread, if any background operations are still running
`async` can be of help (e.g. for cache warming, etc).
:::
**
- `resourceLimits`: (`object`) See [Node.js new Worker options](https://nodejs.org/api/worker_threads.html#worker_threads_new_worker_filename_options)
- `maxOldGenerationSizeMb`: (`number`) The maximum size of each worker threads
main heap in MB.
- `maxYoungGenerationSizeMb`: (`number`) The maximum size of a heap space for
recently created objects.
- `codeRangeSizeMb`: (`number`) The size of a pre-allocated memory range used
for generated code.
- `stackSizeMb` : (`number`) The default maximum stack size for the thread.
Small values may lead to unusable Worker instances. Default: 4
- `env`: (`object`) If set, specifies the initial value of `process.env` inside
the worker threads. See [Node.js new Worker options](https://nodejs.org/api/worker_threads.html#worker_threads_new_worker_filename_options) for details.
- `argv`: (`any[]`) List of arguments that will be stringified and appended to
`process.argv` in the worker. See [Node.js new Worker options](https://nodejs.org/api/worker_threads.html#worker_threads_new_worker_filename_options) for details.
- `execArgv`: (`string[]`) List of Node.js CLI options passed to the worker.
See [Node.js new Worker options](https://nodejs.org/api/worker_threads.html#worker_threads_new_worker_filename_options) for details.
- `workerData`: (`any`) Any JavaScript value that can be cloned and made
available as `require('piscina').workerData`. See [Node.js new Worker options](https://nodejs.org/api/worker_threads.html#worker_threads_new_worker_filename_options)
for details. Unlike regular Node.js Worker Threads, `workerData` must not
specify any value requiring a `transferList`. This is because the `workerData`
will be cloned for each pooled worker.
- `taskQueue`: (`TaskQueue`) By default, Piscina uses a first-in-first-out
queue for submitted tasks. The `taskQueue` option can be used to provide an
alternative implementation. See [Custom Task Queues](https://github.com/piscinajs/piscina#custom_task_queues) for additional detail.
- `niceIncrement`: (`number`) An optional value that decreases priority for
the individual threads, i.e. the higher the value, the lower the priority
of the Worker threads. This value is used on Unix/Windows and requires the
optional [`@napi-rs/nice`](https://npmjs.org/package/@napi-rs/nice) module to be installed.
See [`nice(2)`](https://linux.die.net/man/2/nice) and [`SetThreadPriority`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setthreadpriority) for more details.
- `trackUnmanagedFds`: (`boolean`) An optional setting that, when `true`, will
cause Workers to track file descriptors managed using `fs.open()` and
`fs.close()`, and will close them automatically when the Worker exits.
Defaults to `true`. (This option is only supported on Node.js 12.19+ and
all Node.js versions higher than 14.6.0).
- `closeTimeout`: (`number`) An optional time (in milliseconds) to wait for the pool to
complete all in-flight tasks when `close()` is called. The default is `30000`
- `recordTiming`: (`boolean`) By default, run and wait time will be recorded
for the pool. To disable, set to `false`.
- `workerHistogram`: (`boolean`) By default `false`. It will hint the Worker pool to record statistics for each individual Worker
- `loadBalancer`: ([`PiscinaLoadBalancer`](#piscinaloadbalancer)) By default, Piscina uses a least-busy algorithm. The `loadBalancer`
option can be used to provide an alternative implementation. See [Custom Load Balancers](../advanced-topics/loadbalancer.mdx) for additional detail.
- `workerHistogram`: (`boolean`) By default `false`. It will hint the Worker pool to record statistics for each individual Worker
- `loadBalancer`: ([`PiscinaLoadBalancer`](#piscinaloadbalancer)) By default, Piscina uses a least-busy algorithm. The `loadBalancer`
option can be used to provide an alternative implementation. See [Custom Load Balancers](../advanced-topics/loadbalancer.mdx) for additional detail.
- `resourceLimits`: (`object`) See [Node.js new Worker options](https://nodejs.org/api/worker_threads.html#worker_threads_new_worker_filename_options)
- `maxOldGenerationSizeMb`: (`number`) The maximum size of each worker threads
main heap in MB.
- `maxYoungGenerationSizeMb`: (`number`) The maximum size of a heap space for
recently created objects.
- `codeRangeSizeMb`: (`number`) The size of a pre-allocated memory range used
for generated code.
- `stackSizeMb` : (`number`) The default maximum stack size for the thread.
Small values may lead to unusable Worker instances. Default: 4
- `env`: (`object`) If set, specifies the initial value of `process.env` inside
the worker threads. See [Node.js new Worker options](https://nodejs.org/api/worker_threads.html#worker_threads_new_worker_filename_options) for details.
- `argv`: (`any[]`) List of arguments that will be stringified and appended to
`process.argv` in the worker. See [Node.js new Worker options](https://nodejs.org/api/worker_threads.html#worker_threads_new_worker_filename_options) for details.
- `execArgv`: (`string[]`) List of Node.js CLI options passed to the worker.
See [Node.js new Worker options](https://nodejs.org/api/worker_threads.html#worker_threads_new_worker_filename_options) for details.
- `workerData`: (`any`) Any JavaScript value that can be cloned and made
available as `require('piscina').workerData`. See [Node.js new Worker options](https://nodejs.org/api/worker_threads.html#worker_threads_new_worker_filename_options)
for details. Unlike regular Node.js Worker Threads, `workerData` must not
specify any value requiring a `transferList`. This is because the `workerData`
will be cloned for each pooled worker.
- `taskQueue`: (`TaskQueue`) By default, Piscina uses a first-in-first-out
queue for submitted tasks. The `taskQueue` option can be used to provide an
alternative implementation. See [Custom Task Queues](https://github.com/piscinajs/piscina#custom_task_queues) for additional detail.
- `niceIncrement`: (`number`) An optional value that decreases priority for
the individual threads, i.e. the higher the value, the lower the priority
of the Worker threads. This value is used on Unix/Windows and requires the
optional [`@napi-rs/nice`](https://npmjs.org/package/@napi-rs/nice) module to be installed.
See [`nice(2)`](https://linux.die.net/man/2/nice) and [`SetThreadPriority`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setthreadpriority) for more details.
- `trackUnmanagedFds`: (`boolean`) An optional setting that, when `true`, will
cause Workers to track file descriptors managed using `fs.open()` and
`fs.close()`, and will close them automatically when the Worker exits.
Defaults to `true`. (This option is only supported on Node.js 12.19+ and
all Node.js versions higher than 14.6.0).
- `closeTimeout`: (`number`) An optional time (in milliseconds) to wait for the pool to
complete all in-flight tasks when `close()` is called. The default is `30000`
- `recordTiming`: (`boolean`) By default, run and wait time will be recorded
for the pool. To disable, set to `false`.
- `workerHistogram`: (`boolean`) By default `false`. It will hint the Worker pool to record statistics for each individual Worker
- `loadBalancer`: ([`PiscinaLoadBalancer`](#piscinaloadbalancer)) By default, Piscina uses a least-busy algorithm. The `loadBalancer`
option can be used to provide an alternative implementation. See [Custom Load Balancers](../advanced-topics/loadbalancer.mdx) for additional detail.
- `workerHistogram`: (`boolean`) By default `false`. It will hint the Worker pool to record statistics for each individual Worker
- `loadBalancer`: ([`PiscinaLoadBalancer`](#piscinaloadbalancer)) By default, Piscina uses a least-busy algorithm. The `loadBalancer`
option can be used to provide an alternative implementation. See [Custom Load Balancers](../advanced-topics/loadbalancer.mdx) for additional detail.

:::caution
Use caution when setting resource limits. Setting limits that are too low may
Expand Down Expand Up @@ -191,7 +190,7 @@ type PiscinaHistogramSummary = {
p99_9: number;
p99_99: number;
p99_999: number;
}
};
```

## `PiscinaLoadBalancer`
Expand Down Expand Up @@ -239,6 +238,7 @@ interface PiscinaWorker {
### Example: Custom Load Balancer

#### JavaScript

<a id="custom-load-balancer-example-js"> </a>

```js
Expand Down Expand Up @@ -279,6 +279,7 @@ piscina
```

#### TypeScript

<a id="custom-load-balancer-example-ts"> </a>

```ts
Expand Down
77 changes: 44 additions & 33 deletions docs/docs/api-reference/method.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@
id: Methods
sidebar_position: 3
---
## Method: `run(task[, options])`

## Method: `run<T>(task[, options]): T | Redeable`

Schedules a task to be run on a Worker thread.

* `task`: Any value. This will be passed to the function that is exported from
- `task`: Any value. This will be passed to the function that is exported from
`filename`.
* `options`:
* `transferList`: An optional lists of objects that is passed to
- `options`:
- `transferList`: An optional lists of objects that is passed to
[`postMessage()`] when posting `task` to the Worker, which are transferred
rather than cloned.
* `filename`: Optionally overrides the `filename` option passed to the
- `filename`: Optionally overrides the `filename` option passed to the
constructor for this task. If no `filename` was specified to the constructor,
this is mandatory.
* `name`: Optionally overrides the exported worker function used for the task.
* `abortSignal`: An `AbortSignal` instance. If passed, this can be used to
- `name`: Optionally overrides the exported worker function used for the task.
- `abortSignal`: An `AbortSignal` instance. If passed, this can be used to
cancel a task. If the task is already running, the corresponding `Worker`
thread will be stopped.
(More generally, any `EventEmitter` or `EventTarget` that emits `'abort'`
Expand All @@ -29,32 +30,42 @@ an error, the returned `Promise` will be rejected with that error.
If the task is aborted, the returned `Promise` is rejected with an error
as well.

## Method: `runTask(task[, transferList][, filename][, abortSignal])`
In case the worker return an `AsyncGenerator` or a `Generator`, the `run` method
will resolve with a [`Readable`](https://nodejs.org/api/stream.html#class-streamreadable) stream that will emit the values produced by the generator.

**Deprecated** -- Use `run(task, options)` instead.
:::caution
The return of a `Redeable` stream is experimental and is subject to further changes
:::

Schedules a task to be run on a Worker thread.
**Example**

* `task`: Any value. This will be passed to the function that is exported from
`filename`.
* `transferList`: An optional lists of objects that is passed to
[`postMessage()`] when posting `task` to the Worker, which are transferred
rather than cloned.
* `filename`: Optionally overrides the `filename` option passed to the
constructor for this task. If no `filename` was specified to the constructor,
this is mandatory.
* `signal`: An [`AbortSignal`][] instance. If passed, this can be used to
cancel a task. If the task is already running, the corresponding `Worker`
thread will be stopped.
(More generally, any `EventEmitter` or `EventTarget` that emits `'abort'`
events can be passed here.) Abortable tasks cannot share threads regardless
of the `concurrentTasksPerWorker` options.
`worker.js`

This returns a `Promise` for the return value of the (async) function call
made to the function exported from `filename`. If the (async) function throws
an error, the returned `Promise` will be rejected with that error.
If the task is aborted, the returned `Promise` is rejected with an error
as well.
```js
module.exports = function* (length) {
for (let i = 0; i < length; i++) {
yield `${i}`;
}
};
```

`main.js`

```js
const { resolve } = require('node:path');
const { Piscina } = require('piscina');

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

(async () => {
const stream = pool.run(10);
for await (const value of stream) {
console.log(value);
}
})();
```

## Method: `destroy()`

Expand All @@ -64,9 +75,9 @@ This returns a `Promise` that is fulfilled once all threads have stopped.

## Method: `close([options])`

* `options`:
* `force`: A `boolean` value that indicates whether to abort all tasks that
are enqueued but not started yet. The default is `false`.
- `options`:
- `force`: A `boolean` value that indicates whether to abort all tasks that
are enqueued but not started yet. The default is `false`.

It stops all Workers gracefully.

Expand All @@ -75,4 +86,4 @@ have completed and all threads have stopped.

This method is similar to `destroy()`, but with the difference that `close()`
will wait for the worker tasks to finish, while `destroy()`
will abort them immediately.
will abort them immediately.
Loading