npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

electron-threads

v1.0.2

Published

Electron thread workers using BrowserWindow headless windows

Downloads

49

Readme

Electron Thread (v14 or higher)

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";
const mainWindow = new BrowserWindow();
// initialise using your relative path to child.thread.js and resolve the path with require.resolve(), also add the root/main window for context
let electronThread = new ElectronThread({
    module: require.resolve('./child.worker')
}, mainWindow);

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