Giter VIP home page Giter VIP logo

multee's Introduction

CI Maintainability

multee

multee is a "battery" API. It turns node's multitasking modules, namingly child_process and worker_threads, into simple async functions.

Why multee helps

Without multee, you need to listen to messages from your threads/processes, and it is hard to integrate the listener to other part of your code. Also, when there are multiple operations inside the worker, we have to implement the dispatching logic inside the message listener.

The code will look like below without multee

// worker.js
process.on('message', (msg) => {
  // do heavy load job
  let result = 0
  for (let i = 0; i < 100000000; i++) {
    result += heavy_and_return_same(i)
  }
  process.send(result)
})

// master.js
const child = fork('./worker')

process.on('message', (msg) => {
  // if is job result
  part2(msg)
})

function part1() {
  child.send(payload_for_worker)
}

function part2(result) {
  // do the rest with result
}

And with multee, it's just as easy as calling an async function.

// worker.js
const Multee = require('multee')
const multee = Multee('worker') // worker_threads, use 'child' for child_process

export const jobA = multee.createHandler('jobA', () => {
  // do the heavy load here
  let result = 0
  for (let i = 0; i < 100; i++) {
    result += heavy_and_return_same(i)
  }
  return result
})

module.exports = () => {
  const worker = multee.start(__filename)
  return { result: jobA(worker) }
}

// master.js
async function partA() {
  const worker = require('./worker')
  const result = await worker.jobA()
  // do the rest with result
  console.log(result)
  // { result: 4950 }
}

Out-of-the-box Typescript support

multee works with Typescript. As you can't directly start worker_threads from Typescript, multee includes the battery to handle that. note: ts-node needed as a peer dependency when using Typescript.

License

MIT

multee's People

Contributors

rjyo avatar dependabot[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.