2.0.13 • Published 4 years ago

subprocess-robot v2.0.13

Weekly downloads
30
License
Apache-2.0
Repository
github
Last release
4 years ago

SubProcess Robot

Create subprocesses and deal with messaging. Good for delegating tasks to a differnet process

Import a function and run it using a child process

Before:

import { slowSynchronousTask } from './synchronous-tasks';

export function synchronousTask(options) {
    return slowSynchronousTask(options);
}

After:

import { spawnProcess } from 'subprocess-robot';

export async function asynchronousTask(options) {
    const { slowSynchronousTask } =
        await spawnProcess.import(require.resolve('./synchronous-tasks'));

    return await slowSynchronousTask(options);
}

Load balance your task between a pool of processes

Before:

import { slowSynchronousTask } from './synchronous-tasks';

export function synchronousTask(options) {
    return slowSynchronousTask(options);
}

After:

import { spawnProcessPool } from 'subprocess-robot';

export async function asynchronousTask(options) {
    const { slowSynchronousTask } =
        await spawnProcessPool.import(require.resolve('./synchronous-tasks'));

    return await slowSynchronousTask(options);
}

Manually create a subprocess and send messages between

Parent process:

import { spawnProcess } from 'subprocess-robot';

const childProcess = spawnProcess({
    script: require.resolve('./child')
});

childProcess.on('getUser', ({ id ) => {
    return {
        id,
        name: 'Daniel',
        logout() {
            // log the user out
        }
    }
});

Child process:

import { attachProcess } from 'subprocess-robot';

const parentProcess = attachProcess();

let user = await parentProcess.send('getUser', { id: 1337 });

console.log(`Logging ${ user.name } out`);
await user.logout();

Create a pool of processes and delegate tasks

Parent process:

import { spawnProcessPool } from 'subprocess-robot';

const childProcessPool = spawnProcessPool({
    script: require.resolve('./child')
});

let result = childProcessPool.send('do_some_blocking_task', data);

Child process:

import { attachProcess } from 'subprocess-robot';

const parentProcess = attachProcess();

parentProcess.on('do_some_blocking_task', data => {
    return slowSynchronousCompile(data);
})

Manually create a pool of processes and import a function

Parent process:

import { spawnProcessPool } from 'subprocess-robot';

const childProcessPool = spawnProcessPool();

let { doSomeBlockingTask } = await childProcessPool.import(require.resolve('./blockingTask'));

let result = await doSomeBlockingTask(config);

Child process:

export function doSomeBlockingTask(config) {
    return slowSynchronousCompile(config);
}

Quick Start

npm install --save subprocess-robot

Tests

  • Run the tests:

    npm test
2.0.13

4 years ago

2.0.12

4 years ago

2.0.11

4 years ago

2.0.10

4 years ago

2.0.9

4 years ago

2.0.6

6 years ago

2.0.5

6 years ago

2.0.4

6 years ago

2.0.3

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.0.19

6 years ago

1.0.18

6 years ago

1.0.16

6 years ago

1.0.15

6 years ago

1.0.14

6 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago