Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@hoth/thread

Package Overview
Dependencies
Maintainers
3
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@hoth/thread

thread pool plugin for hoth framework

  • 1.0.2
  • latest
  • release
  • Source
  • npm
  • Socket score

Version published
Maintainers
3
Created
Source

@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
}

Keywords

FAQs

Package last updated on 24 May 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc