# thread-worker

> create and control threads with worker_threads modules

Latest version **1.0.2** (published 2023-09-18) · ISC license · 0 weekly downloads

## Install

```sh
npm install thread-worker
pnpm add thread-worker
yarn add thread-worker
bun add thread-worker
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2023-09-18 |
| First published | 2022-07-28 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 15.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Wang |
| Maintainers | ant-army |
| Keywords | thread, threads, worker, threadworker, thread-worker, threads-worker, worker-threads |

## Links

- npm: https://www.npmjs.com/package/thread-worker
- Repository: https://gitee.com/daoio/thread-worker
- npm.io page: https://npm.io/package/thread-worker

## Alternatives

- [cron](https://npm.io/package/cron.md) — 4.9M weekly downloads
- [@vercel/queue](https://npm.io/package/@vercel/queue.md) — 731.6K weekly downloads
- [create-sonicjs](https://npm.io/package/create-sonicjs.md) — 1.6K weekly downloads
- [@exellix/jobs-api](https://npm.io/package/@exellix/jobs-api.md) — 941 weekly downloads
- [@forwardimpact/libskill](https://npm.io/package/@forwardimpact/libskill.md) — 575 weekly downloads

## Recent versions

- 1.0.2 (latest) — 2023-09-18
- 1.0.1 — 2023-03-30
- 1.0.0 — 2022-07-28
- 1.0.0-beta4 — 2022-07-28
- 1.0.0-beta3 — 2022-07-28
- 1.0.0-beta2 — 2022-07-28
- 1.0.0-beta — 2022-07-28

## README

# thread-worker

Node.js环境的工作线程扩展，此扩展十分简单，就是封装了worker_threads模块，方便调用。

模块导出只有一个函数，定义如下：

```javascript

/**
 * 
 * @param {string} pathfile 
 * @param {object} options 
 * @returns {worker}
 */

function runWorkerThread(file, options = {})

```

参数可以直接参考Node.js官方文档对new Worker部分的描述。

只是options部分做了扩展，支持以下选项：

| 选项 | 说明 | 是否必须 |
|----|----|----|
| restart | 重启模式，支持always，fail，fail-count；设置为true也认为是always | 否 |
| restartDelay | 重启延时毫秒数 | 否 |
| restartLimit | 针对fail-count模式，失败重启次数限制，若为fail-count模式，但是不设置此选项，则会一直失败重启。 | 否 |
| events | 事件函数，传递key值为对应的事件名称，value为事件函数，此函数做了包装处理，第一个参数是创建的worker对象，后面的参数和事件函数传递参数一致。 | 否 |


## 安装

```javascript

npm i thread-worker

```

## 基本使用

```javascript

'use strict'

const threads = require('thread-worker')

//要通过线程运行的文件以及选项，其参数可以直接参照Node.js官方文档
//如果传递的是文件，要求文件必须是绝对路径，或以 . 开头的相对路径。

//扩展在检测到没有设置eval选项，会自动把a.js格式化为./a.js。

let worker = threads('a.js', {
    //如果你要给a.js传递参数，要使用此选项。
    //请参考官方文档对选项的描述。
    argv: [],

    restart: 'fail',

    restartDelay: 1000,

    events: {
        message: (w, msg) => {
            console.log('get message from', w.threadId, msg)
            await new Promise((rv) => { setTimeout(rv, 1000) })
            w.postMessage({msg: 'ok, recived', time: (new Date).toLocaleString()})
        },

        // w就是worker，扩展做了包装处理，
        // 这样方便拿到worker对象。
        exit: (w, code) => {
            console.log(w.threadId, code, 'exit')
        }
    }
})

worker.on('online', () => {
    console.log(worker.threadId, 'running')
})

worker.postMessage({
    tid: worker.threadId,
    say: 'ok'
})


```

在a.js文件中，存在以下代码：

```javascript

'use strict'

const { parentPort } = require('node:worker_threads')

parentPort.on('message', (msg) => {

    console.log('get message', msg)

    parentPort.postMessage({
        pid: process.pid,
        rand: Math.random()
    })

})


```

---
_Source: https://npm.io/package/thread-worker · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
