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
And a renderer file where we call:
# Import the ElectronThread class import
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: length maxCallTime: Infinity maxRetries: 10
-
module
You should use an absolute path to the module file, the best way to obtain the path is withrequire.resolve('./path/to/module')
. -
options.windowOptions
allows you to customize all the parameters passed to BrowserWindowConstructorOptions. This object supports all possible options ofBrowserWindowConstructorOptions
. -
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, including1
. -
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 aTimeoutError
(checkerr.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 toInfinity
which means that each call of each terminated worker will always be auto requeued. When the number of retries exceedsmaxRetries
value, the job callback will be executed with aProcessTerminatedError
.
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
Inspiration
- worker-farm - Worker Farm
- workerpool - Workerpool