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

v1.0.2

Published

create and control threads with worker_threads modules

Downloads

4

Readme

thread-worker

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

模块导出只有一个函数,定义如下:


/**
 * 
 * @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对象,后面的参数和事件函数传递参数一致。 | 否 |

安装


npm i thread-worker

基本使用


'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文件中,存在以下代码:


'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()
    })

})