Git Product home page Git Product logo

electron-thread's Introduction

Electron Thread

Electron workers using BrowserWindow headless window

This package allows you to use multithreading in Electron. This type of multithreading allows you to use NODE JS API and Electron API

Note: this module has to be used in the renderer process and the thread to be invoked in the renderer process. Also make sure you register the module in the main module import 'electron-thread' and also import import * as et from "electron-thread" like this or if you prefer to import classes separetely, you have to register the module as well import 'electron-thread';

Install

npm install --save electron-thread

Example

Given a file in renderer, child.worker.js:

# Import the ThreadExport class
import { ThreadExport } from "electron-thread";

# Write your methods
function getProcessId(paramOne, paramTwo) {
    return `${paramOne}:${paramTwo} ${process.pid}`;
}

# Register your methods
ThreadExport.export({
    getProcessId: getProcessId
});

And a renderer file where we call:

# Import the ElectronThread class
import { ElectronThread } from "electron-thread";

# initialise using your relative path to child.thread.js and resolve the path with require.resolve()
let electronThread = new ElectronThread({
    module: require.resolve('./child.worker')
});

let test = async () => {
  for (var i = 0; i < 10; i++) {
      let r = electronThread.run<string>({
          method: 'getProcessId',
          parameters: ['#', i + 1]
      });
      r
      .then(r => console.log(r))
      .catch(e => console.log(e));
  }
}

test();

We'll get an output something like the following:

"#:1 13560"
"#:2 21980"
"#:3 21868"
"#:4 22712"
"#:5 2476"
"#:6 15936"
"#:7 19140"
"#:8 14928"
"#:9 12992"
"#:10 22132"

API

Electron thread exports a main method run(options: IThreadRunOptions) and an end() method. The run() method sets up a "thread farm" of coordinated BrowserWindows.

ElectronThread(options: IThreadLaunchOptions)

options: IThreadLaunchOptions

If you don't provide an options object then the following defaults will be used:

{
    module              : string,
    options             :
                            {
                                windowOptions: BrowserWindowConstructorOptions,
                                maxConcurrentThreads: require('os').cpus().length,
                                maxCallTime: Infinity,
                                maxRetries: 10
                            }
}
  • module You should use an absolute path to the module file, the best way to obtain the path is with require.resolve('./path/to/module').

  • options.windowOptions allows you to customize all the parameters passed to BrowserWindowConstructorOptions. This object supports all possible options of BrowserWindowConstructorOptions.

  • options.maxConcurrentThreads will set the number of child processes to maintain concurrently. By default it is set to the number of CPUs available on the current system, but it can be any reasonable number, including 1.

  • options.maxCallTime when set, will cap a time, in milliseconds, that any single call can take to execute in a worker. If this time limit is exceeded by just a single call then the worker running that call will be killed and any calls running on that worker will have their callbacks returned with a TimeoutError (check err.type == 'TimeoutError').

  • options.maxCallTime allows you to control the max number of call retries after worker termination (unexpected or timeout). By default this option is set to Infinity which means that each call of each terminated worker will always be auto requeued. When the number of retries exceeds maxRetries value, the job callback will be executed with a ProcessTerminatedError.

You initialize electron "thread farm" let electronThread ElectronThread(options: IThreadLaunchOptions).

electronThread.end()

It will close all threads and won't wait for the task to complete.

electronThread.run(options: IThreadRunOptions)

options: IThreadRunOptions

You have to specify the exported method name and the arguments

{
    method              : string,
    parameters          : any []
}

ElectronThread(methods: Object)

In the worker file we export the methods

{
    exportMethodName1   : method1,
    exportMethodName2   : method2,
    exportMethodNameN   : methodN,
}
# Import the ThreadExport class
import { ThreadExport } from "electron-thread";

# Write your methods
function getProcessId(paramOne, paramTwo) {
    return `${paramOne}:${paramTwo} ${process.pid}`;
}

# Register your methods
ThreadExport.export({
    getProcessId: getProcessId
});

Inspiration

electron-thread's People

Contributors

thecele avatar vanessayuenn avatar malept avatar zeke avatar worldsayshi avatar ckerr avatar vhashimotoo avatar leonardonline avatar codebytere avatar dpavlou avatar naofumi-fujii avatar

Stargazers

 avatar LG avatar howard liang avatar

Watchers

James Cloos avatar

Forkers

venipa

electron-thread's Issues

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.