All Projects → f5io → pool

f5io / pool

Licence: MIT license
A highly flexible process pooling library for Node.js

Programming Languages

typescript
32286 projects
shell
77523 projects

Projects that are alternatives of or similar to pool

So 5 5
SObjectizer: it's all about in-process message dispatching!
Stars: ✭ 87 (+383.33%)
Mutual labels:  csp, concurrency
csp.js
📺 CSP for vanilla JavaScript
Stars: ✭ 45 (+150%)
Mutual labels:  csp, concurrency
Libcsp
A concurrency C library 10x faster than Golang.
Stars: ✭ 1,160 (+6344.44%)
Mutual labels:  csp, concurrency
Aiochan
CSP-style concurrency for Python
Stars: ✭ 116 (+544.44%)
Mutual labels:  csp, concurrency
Chymyst Core
Declarative concurrency in Scala - The implementation of the chemical machine
Stars: ✭ 142 (+688.89%)
Mutual labels:  csp, concurrency
SOMns
SOMns: A Newspeak for Concurrency Research
Stars: ✭ 62 (+244.44%)
Mutual labels:  csp, concurrency
Sobjectizer
An implementation of Actor, Publish-Subscribe, and CSP models in one rather small C++ framework. With performance, quality, and stability proved by years in the production.
Stars: ✭ 172 (+855.56%)
Mutual labels:  csp, concurrency
csp
A library for Communicating Sequential Processes in Node.js, built on top of async/await
Stars: ✭ 59 (+227.78%)
Mutual labels:  csp, concurrency
concore
Core abstractions for dealing with concurrency in C++
Stars: ✭ 57 (+216.67%)
Mutual labels:  concurrency
go course
個人多年來學習與實作上的心得筆記
Stars: ✭ 25 (+38.89%)
Mutual labels:  concurrency
python3-concurrency
Python3爬虫系列的理论验证,首先研究I/O模型,分别用Python实现了blocking I/O、nonblocking I/O、I/O multiplexing各模型下的TCP服务端和客户端。然后,研究同步I/O操作(依序下载、多进程并发、多线程并发)和异步I/O(asyncio)之间的效率差别
Stars: ✭ 49 (+172.22%)
Mutual labels:  concurrency
pyGRETA
python Generator of REnewable Time series and mAps
Stars: ✭ 27 (+50%)
Mutual labels:  csp
flash-sale
高并发多方案秒杀架构-核心应用。
Stars: ✭ 90 (+400%)
Mutual labels:  concurrency
hunt
A refined core library for D programming language. The module has concurrency / collections / event / io / logging / text / serialization and more.
Stars: ✭ 86 (+377.78%)
Mutual labels:  concurrency
haxe-concurrent
A haxelib for basic platform-agnostic concurrency support
Stars: ✭ 69 (+283.33%)
Mutual labels:  concurrency
nuxt-security
Module for Nuxt.js to configure security headers and more
Stars: ✭ 46 (+155.56%)
Mutual labels:  csp
softpool
SoftPoolNet: Shape Descriptor for Point Cloud Completion and Classification - ECCV 2020 oral
Stars: ✭ 62 (+244.44%)
Mutual labels:  pooling
think-async
🌿 Exploring cooperative concurrency primitives in Python
Stars: ✭ 178 (+888.89%)
Mutual labels:  concurrency
scalable-concurrent-containers
High performance containers and utilities for concurrent and asynchronous programming
Stars: ✭ 101 (+461.11%)
Mutual labels:  concurrency
optimistic lock coupling rs
🍋: A General Lock following paper "Optimistic Lock Coupling: A Scalable and Efficient General-Purpose Synchronization Method"
Stars: ✭ 21 (+16.67%)
Mutual labels:  concurrency

@f5io/pool

A highly flexible process pooling library for Node.js. Built with @f5io/csp.

npm version

Installation

$ npm install --save @f5io/pool

or

$ yarn add @f5io/pool

API

This library exposes a single factory method for creating pools.

createPool({ poolSize = 5, createProcess, createAsyncProcess, handler }) -> Pool|Promise<Pool>

The pool factory takes an options object containing 3 of 4 properties:

  • poolSize - defaults to 5, determines the size of the pool
  • createProcess - defines a process factory function which can return anything
  • createAsyncProcess - defines an async process factory which can return anything, useful if your process requires time to become active.
  • handler(process, input) -> Promise - defines a function which handles a unit of work. The handler must return a Promise and receives a process (as defined by the process factory) and the input from a call to run on the pool

You must supply only one of createProcess or createAsyncProcess! If you supply createAsyncProcess the return value of the createPool factory will be a Promise<Pool>.

A returned Pool exposes 2 methods:

  • Pool.run(input) -> Promise - The interface defined to run against the pool of processes, supplied input can be of any type as the handler supplied at pool creation defines how the input interacts which the underlying process
  • Pool.close -> Promise - A mechanism for destroying the pool when it is no longer needed

Example Usage

Network I/O parallelisation

By defining our process as a plain Symbol, or true for that matter, we can limit request parallelisation to the size of the pool.

const assert = require('assert');
const fetch = require('node-fetch');
const createPool = require('@f5io/pool');

const { run, close } = createPool({
  poolSize: 2,
  createProcess: () => Symbol('process'),
  handler: (_, query) => {
    console.log(`🚀  running request with query: ${query}`);
    return fetch(`https://postman-echo.com/get?q=${query}`)
      .then(res => res.json())
      .then(res => assert.equal(res.args.q, query));
      .then(_ => console.log(`👌  request completed successfully`));
  }
});

(async () => {
  const queries = Array.from({ length: 20 }, (_, i) => run(`${++i}`));
  await Promise.all(queries);
  close();
})();

request parallelisation

Child process pooling

For spawning multiple child processes and spreading work across processes in the pool.

const assert = require('assert');
const { spawn } = require('child_process');
const createPool = require('@f5io/pool');

const { run, close } = createPool({
  poolSize: 10,
  createProcess: () => {
    const p = spawn('cat', [ '-' ]);
    p.stdin.setEncoding('utf-8');
    p.stdout.setEncoding('utf-8'); 
    return p;
  },
  handler: (p, input) =>
    new Promise(resolve => {
      p.stdout.once('data', d => {
        assert(d, input);
        console.log(`👌  received data: ${d.trim()} from pid: ${p.pid}`);
        resolve(d);
      });
      console.log(`🚀  sending data: ${input.trim()} to pid: ${p.pid}`);
      p.stdin.write(input);
    }),   
});

(async () => {
  const inputs = Array.from({ length: 100 }, (_, i) => run(`${++i}\n`));
  await Promise.all(inputs);
  close();
})();

child process pool

Contributions

Contributions are welcomed and appreciated!

  1. Fork this repository.
  2. Make your changes, documenting your new code with comments.
  3. Submit a pull request with a sane commit message.

Feel free to get in touch if you have any questions.

License

Please see the LICENSE file for more information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].