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

dl-worker

v1.1.0

Published

多线程工具

Downloads

5

Readme

说明

更新日期:2024-03-25

版本:1.1.0

更新内容:

  • 新增子线程监听事件

使用案例

主线程

index.ts

import { WorkerPool } from "dl-worker"; // 或 import WorkerPool from "dl-worker";
const path = require("path");

// 初始化线程池配置,并返回线程池实例
const createTest = WorkerPool.init({
  key: "test",
  size: 2,
  scriptPath: path.join(__dirname, "/worker.js"),
  argument: {
    param: "全局参数6666666",
  },
});

// 获取线程池实例,等同于 createTest 实例
const test = WorkerPool.getWorker("test");

// 线程结束
test.on("exit", (value) => {
  console.log("exit", value);
});

// 线程错误
test.on("error", (value) => {
  console.log("error", value);
});

let count = 0;
for (let i = 0; i < 10; i++) {
  // 执行任务
  test.runTask({ id: "100" + i, money: 288 }, (resp) => {
    count += 1;
    // 执行结果
    console.log(`任务执行${count}===`, resp);

    // 完成所有任务
    if (count == 10) {
      console.log("任务执行完成");
      // 销毁线程池释放资源
      test.destroy();
    }
  });
}

子线程

worker.ts

import { WorkerPool } from "dl-worker"; // 或 import WorkerPool from "dl-worker";
import { workerData } from "worker_threads";

// data 为接收到runTask方法的第一个参数
WorkerPool.on("message", function (data) {
  console.log("接收到了主线程参数:", data);
  // 打折
  data.money = (data.money * Math.random()).toFixed(2) + "元";
  // 发送处理结果给主进程里的runTask的’callback‘方法回调
  this.postMessage(data);
});

WorkerPool.on("messageerror", (error) => {
  console.log("接收到了错误信息:", error);
});

WorkerPool.on("close", () => {
  console.log("当前进程关闭了");
});

console.log("初始化完成");

console.log("获取全局变量:", workerData);