npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

thread-worker-group

v1.0.3

Published

thread worker group

Downloads

4

Readme

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();