Giter Site home page Giter Site logo

acus's Introduction

Acus.js

Utilities for building multi-threaded applications in NodeJS

Installation

npm i -s acus

Available Utilities

WorkerModule

Worker modules allow you to create and declare exported module functions which should run in a separate thread. This is useful for CPU intensive tasks that you want to run in the background.

Usage example

// --- yourModuleB.js ---
const { WorkerModule } = require('acus');
const define = WorkerModule(__filename);

const yourFunction = define(async (paramA, paramB) => {
  // do something with your function...
  return yourResult;
});

module.exports = yourFunction;
// --- yourModuleB.js ---

// -=-=-=-=-=-=-=-=-=-=-=-

// --- yourModuleA.js ---
const yourFunction = require('./yourModuleB');

async function someAsyncFunction() {
  // call yourFunction as any other function
  // it will run in a separate thread
  const result = await yourFunction(paramA, paramB);
  // do something with your result...
}
// --- yourModuleA.js ---

SharedObject

Shared objects allow you to share data pointers between threads. This is useful for sharing large data structures between threads without having to copy them. Changes to the shared object will be reflected in all threads.

Usage example

// --- yourModuleA.js ---
const { SharedObject } = require('acus');
const yourOtherFunction = require('./yourModuleB');

async function someAsyncFunction() {
  const myData = { foo: 'text' };
  const sharedObject = new SharedObject({ object: myData });

  // use getObject() to get the shared object value
  console.log(sharedObject.getObject()); // { foo: 'text' }

  // mutate the shared object in a separate thread
  // pass the object buffer to the function, this acts as a pointer to the object
  await yourOtherFunction(sharedObject.getBuffer());

  console.log(sharedObject.getObject()); // { foo: 'bar' }
}
// --- yourModuleA.js ---

// -=-=-=-=-=-=-=-=-=-=-=-

// --- yourModuleB.js ---
const { WorkerModule, SharedObject } = require('acus');
const define = WorkerModule(__filename);

const yourOtherFunction = define(async (buffer) => {
  // read the shared object from the buffer pointer
  const sharedObject = new SharedObject({ buffer });

  // mutate the shared object
  sharedObject.setObject({ foo: 'bar' });
});

module.exports = yourOtherFunction;
// --- yourModuleB.js ---

License

MIT

acus's People

Contributors

doriandres avatar

Stargazers

Trua Shamu avatar

Watchers

 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.