Skip to content

Commit

Permalink
feat(backport): use @napi-rs/nice to support Windows (#655) (#660)
Browse files Browse the repository at this point in the history
Co-authored-by: LongYinan <lynweklm@gmail.com>
  • Loading branch information
metcoder95 and Brooooooklyn authored Sep 18, 2024
1 parent 802f4d2 commit 541295d
Show file tree
Hide file tree
Showing 9 changed files with 332 additions and 60 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,9 @@ This class extends [`EventEmitter`][] from Node.js.
alternative implementation. See [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 only used on Linux and requires the
optional [`nice-napi`][] module to be installed.
See [`nice(2)`][] for more details.
of the Worker threads. This value is used on Unix/Windows and requires the
optional [`@napi-rs/nice`][https://www.npmjs.com/package/@napi-rs/nice] module to be installed.
See [`nice(2)`][https://linux.die.net/man/2/nice] 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.
Expand Down Expand Up @@ -842,8 +842,8 @@ are no one set of options that are going to work best.
On Linux systems that support [`nice(2)`][], Piscina is capable of setting
the priority of every worker in the pool. To use this mechanism, an additional
optional native addon dependency (`nice-napi`, `npm i nice-napi`) is required.
Once [`nice-napi`][] is installed, creating a `Piscina` instance with the
optional native addon dependency (`@napi-rs/nice`, `npm i @napi-rs/nice`) is required.
Once [`@napi-rs/nice`][] is installed, creating a `Piscina` instance with the
`niceIncrement` configuration option will set the priority for the pool:
```js
Expand Down Expand Up @@ -988,7 +988,7 @@ Piscina development is sponsored by [NearForm Research][].
[`postMessage`]: https://nodejs.org/api/worker_threads.html#worker_threads_port_postmessage_value_transferlist
[`examples/task-queue`]: https://github.com/jasnell/piscina/blob/master/examples/task-queue/index.js
[`nice(2)`]: https://linux.die.net/man/2/nice
[`nice-napi`]: https://npmjs.org/package/nice-napi
[`@napi-rs/nice`]: https://npmjs.org/package/@napi-rs/nice
[`runTime`]: #property-runtime-readonly
[Custom Task Queues]: #custom_task_queues
[ES modules]: https://nodejs.org/api/esm.html
Expand Down
17 changes: 10 additions & 7 deletions docs/docs/advanced-topics/performance.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,21 @@ are no one set of options that are going to work best.

### Thread priority on Linux systems

On Linux systems that support [`nice(2)`][], Piscina is capable of setting
On Unix systems that support [`nice(2)`][https://linux.die.net/man/2/nice] and Windows, Piscina is capable of setting
the priority of every worker in the pool. To use this mechanism, an additional
optional native addon dependency (`nice-napi`, `npm i nice-napi`) is required.
Once [`nice-napi`][] is installed, creating a `Piscina` instance with the
`niceIncrement` configuration option will set the priority for the pool:
optional native addon dependency (`@napi-rs/nice`, `npm i @napi-rs/nice`) is required.
Once [`@napi-rs/nice`][https://www.npmjs.com/package/@napi-rs/nice] is installed, creating a `Piscina`instance with the`niceIncrement` configuration option will set the priority for the pool:

```js
const Piscina = require('piscina');
const Piscina = require('piscina')
const { WindowsThreadPriority } = require('@napi-rs/nice')
const pool = new Piscina({
worker: '/absolute/path/to/worker.js',
niceIncrement: 20
});
niceIncrement:
process.platform !== 'win32'
? 20
: WindowsThreadPriority.ThreadPriorityHighest,
})
```

The higher the `niceIncrement`, the lower the CPU scheduling priority will be
Expand Down
6 changes: 3 additions & 3 deletions docs/docs/api-reference/class.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ This class extends [`EventEmitter`](https://nodejs.org/api/events.html) from Nod
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 only used on Linux and requires the
optional [`nice-napi`](https://npmjs.org/package/nice-napi) module to be installed.
See [`nice(2)`](https://linux.die.net/man/2/nice) for more details.
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.
Expand Down
Loading

0 comments on commit 541295d

Please sign in to comment.