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

@hoth/thread

v1.0.2

Published

thread pool plugin for hoth framework

Downloads

4

Readme

@hoth/thread

thread worker pool based on piscina.

使用方式

main

  • fastify 插件

    该插件会在 fastify 实例上新增两个属性,内部使用 fastify-plugin 来包裹,因此会在全局实例上增加:

    • fastify.piscina {Piscina} Piscina 实例
    • fastify.runTask() {Function} Piscina runTask 函数
    import {threadPlugin} from '@hoth/thread';
    import fastify from 'fastify';
    import path from 'path';
    
    const app = fastify();
    
    app.register(threadPlugin, {
        threadsNumber: 10,
        filename: path.resolve(__dirname, 'worker.js')
    })
    
    app.get('/', async (request, reply) => {
    reply.send({ hello: `world [${await app.runTask({ a: 1, b: 2 })}]` });
    });
  • initThread

    如果不想在全局实例上增加属性,可以单独调用 initThread 方法:

    import {initThread} from '@hoth/thread';
    const pool = await initThread({
        threadsNumber: 10,
        filename: path.resolve(__dirname, 'worker.js')
    });
    
    // 自己处理 pool 的使用方式
    fastify.decorate('piscina', pool);
    fastify.decorate('runTask', (...args) => pool.runTask(...args));

worker

import {
    workerWrapper,
    workerData
} from '@hoth/thread';

export default workerWrapper(function (args) {
    // use workerData
    // some task
});

异常处理

初始化时

当子线程创建失败,会有异常抛出,需要业务方自行处理。

try {
    await app.register(thread, {
        threadsNumber: 10
    });
}
catch (e) {
    // do something
}

app.register(thread, {
    threadsNumber: 10
});
try {
    await app.listen(3000);
}
catch (e) {
    // do something
}

initThread 时

try {
    const pool = await initThread({
        threadsNumber: 10,
        filename: path.resolve(__dirname, 'worker.js')
    });
}
catch (e) {
    // do something
}

runTask 时

try {
    await fastifyInstance.runTask();
}
catch (e) {
    // do something
}