Skip to content

Commit

Permalink
docs(workerpool): bring up
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Jun 12, 2020
1 parent 2c59812 commit c7de431
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Utilities for [Hexo].
- [truncate](#truncatestr-options)
- [unescapeHTML](#unescapehtmlstr)
- [url_for](#url_forpath-option)
- [WorkerPool](#workerpool)
- [bind(hexo)](#bindhexo)

## Installation
Expand Down Expand Up @@ -665,6 +666,64 @@ url_for('/css/style.css', {relative: false})
// /css/style.css
```

## WorkerPool

A worker management utility designed for multi-core job running.

**`new WorkerPool(workerPath[, numberOfThreads])`**

initializing a WorkerPool.

- `workerPath`: `<string>` The path to `worker.js` file
- `numberOfThreads`: `<number>` The number of workers will be created. Default value is `require('os').cpus().length`.

```js
const { WorkerPool } = require('hexo-util');
const pool = new WorkerPool(workerPath, 2);
```

**`workerpool.run(data)`**

Add tasks to the queue and execute when there is an inactive worker.

- `data`: `<any>` The parameter will be passed to `worker.js` function.
- Return: `<Promise>`

```js
async () => {
const result = await pool.run();
}
```

**`workerpool.destroy(force)`**

Destroy the WorkerPool by terminating every workers created under the WorkerPool.

- `force`: `<boolean>`. Default is `false`.

```js
pool.destroy();
```

If there is a worker is still executing, an Error will be thrown:

```
The worker [id of the worker] is still runing!
```

You can force destroy a workerpool by passing a `true` to it:

```js
pool.destroy(true);
```

----

Also, there are other internal functions. Avoid using them directly.

- `workerpool.getInactiveWorkerId()` - Rerturn the id of the first incactive worker in the workerpool. Return `-1` if no incactive worker is found.
- `workerpool.runWorker(workerId, taskObj)` - Run given task in a specific worker.

## bind(hexo)

Following utilities require `bind(hexo)` / `bind(this)` / `call(hexo, input)` / `call(this, input)` to parse the user config when initializing:
Expand Down

0 comments on commit c7de431

Please sign in to comment.