1 Star 0 Fork 0

linuxmail / node-thread-worker-group

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

node-thread-worker-group

说明

网址: https://gitee.com/linuxmail/node-thread-worker-group

npm i thread-worker-group

注意: 下划线开头的方法/变量不应该使用

接口

// 请求
export interface ServiceRequest {
  // 数据
  data: any
  // 通知消息接收器, 请注意: 不是此"请求"的返回的处理函数
  tipReceiver?: TipReceiver
  // 可以指定 workerId
  workerId?: string
}

// 通知消息接收器
export type TipReceiver = (data: any) => void

// 请求的返回结果
export interface ServiceResponse {
  // 框架上的返回状态, 空表示正常
  status: string
  // 返回的数据
  data: any
}

// 创建worker线程组的 选项
export interface WorkerGroupOptions {
  // 脚本路径
  scriptPathname: string
  // 是否cpu密集型, 如果是cpu密集型则每个worker同时只处理一个请求
  cpuIntensive?: boolean
  // 是否开启 debug
  debug?: boolean
}

// 创建一个worker的选项
export interface WorkerOptions {
  // 可以指定worker的id
  workerId?: string
  // 传递给worker的数据
  workerData?: any
  // node原生的创建worker的选项
  workerOriginalOptions?: any
}

// 统计信息
export interface workerGroupStatistics {
  // 所有请求的数量
  enteredCount: number
  // 已经处理完毕的请求的数量 
  dealedCount: number
}

// worker端, 消息通知发送器
export type ThreadTipSender = (data: any) => void

// worker端, 模块请求
export interface ThreadJobRequest {
  data: any
  tipSender: ThreadTipSender
}

export interface ThreadJobResponse {
  data: any
}

export type ThreadJobHandler = (req: ThreadJobRequest) => Promise<ThreadJobResponse>

方法: 主线程端

const threadWorkerGroup = require("thread-worker-group")

// 返回是否主线程
threadWorkerGroup.isMainThread()

创建worker组

var wg = new threadWorkerGroup.workerGroup({
  scriptPathname: __filename,
  /* cpuIntensive: false, */
})

创建一个worker

wg.createWorker({
  workerId: "id123",
  workerData: "xxx",
})

wg.createWorker({
  workerData: {a: "ccc"},
})

wg.createWorker({ })

发起请求

// 是Promise, 没有 catch
wg.request({
  data: {a: 123, b: "xxx"},
}).then((res:ServiceResponse) => {
  console.log("response:", res.status, res.data)
})

wg.request({
  data: {a: 123, b: "xxx"},
  workerId: "id123",
}).then((res:ServiceResponse) => {
  console.log("response:", res.status, res.data)
})

wg.request({
  data: {a: 123, b: "xxx"},
  tipReceiver: (tip: any) => {
    console.log(tip)
  },
}).then((res:ServiceResponse) => {
  console.log("response:", res.status, res.data)
})

统计信息

var st:workerGroupStatistics = wg.statistics

注册全局通知消息接收器

function handler(data: any) {
  console.log(data)
}
wg.registerGlobalTipReceiver("somename", handler)

方法: 子线程(worker)端

const threadWorkerGroup = require("thread-worker-group")
// 返回是否主线程
threadWorkerGroup.isMainThread()

注册处理函数

// 必须是 async 的
const handler = async (req: ) => {
  var i = 0;
  for (i = 0; i < 100; i++) {
    req.tipSender({ data: i})
    req.tipSender("xxx")
  }
  return { data: "something" }
}

threadWorkerGroup.threadRegisterHandler(handler)

全局通知消息

threadWorkerGroup.threadGlobalTipSender("somename", anydata)

获取 workerData

var d = threadWorkerGroup.threadGetWorkerData();
MIT License Copyright (c) 2024 linuxmail Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

node worker 线程池通讯框架 展开 收起
JavaScript
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
JavaScript
1
https://gitee.com/linuxmail/node-thread-worker-group.git
git@gitee.com:linuxmail/node-thread-worker-group.git
linuxmail
node-thread-worker-group
node-thread-worker-group
master

搜索帮助