Does e-structure support multithreading through web workers?

I have a hard time when I am making an electronic application. Will it be with web workers?

+6
source share
4 answers

During the rendering process, you can create web workers and they will run in their own threads, however, Node integration will be disabled for these web workers , since Node is not thread safe. Therefore, if you want to run something in a separate thread that uses Node, then you will need to create a separate process, you can do this with child_process.fork() and then communicate with the new process using send() .

+14
source

On Electron, Node.js and processes

An electron works on the same principles as Node. In Node.js, threads are not an atomic unit of execution. You work in an event loop, and the default execution is async. See more details . Speaking of the fact that you can deploy several child processes in Node.js by marking them up.

Here is an example.

 //forking a process using cluster var cluster = require('cluster'); var http = require('http'); var numCPUs = 4; if (cluster.isMaster) { for (var i = 0; i < numCPUs; i++) { cluster.fork(); } } else { http.createServer(function(req, res) { res.writeHead(200); res.end('process ' + process.pid + ' says hello!'); }).listen(8000); } 

Credit How to create a Node.js cluster to speed up your applications. for example.

Electronic concepts

Returning to Electron, there are a couple of additional concepts you need to know about. The processes at Electron are unique in that they come in 2 flavors.

** Render Process ** - A process containing a web page. These processes are sandboxed, just like regular web pages.

** The main process ** is the process that loads your application. It creates rendering processes and their web pages. It can also act as a communication hub for all rendering processes rendering through rpc .

Here is part of the main.js sample from the Electronic Tutorial . This is the main process causing the browser window. Pay particular attention to the mainWindow variable.

 'use strict'; const electron = require('electron'); const app = electron.app; // Module to control application life. const BrowserWindow = electron.BrowserWindow; // Module to create native browser window. // Keep a global reference of the window object, if you don't, the window will // be closed automatically when the JavaScript object is garbage collected. var mainWindow = null; // Quit when all windows are closed. app.on('window-all-closed', function() { // On OS X it is common for applications and their menu bar // to stay active until the user quits explicitly with Cmd + Q if (process.platform != 'darwin') { app.quit(); } }); // This method will be called when Electron has finished // initialization and is ready to create browser windows. app.on('ready', function() { // Create the browser window. mainWindow = new BrowserWindow({width: 800, height: 600}); // and load the index.html of the app. mainWindow.loadURL('file://' + __dirname + '/index.html'); // Open the DevTools. mainWindow.webContents.openDevTools(); // Emitted when the window is closed. mainWindow.on('closed', function() { // Dereference the window object, usually you would store windows // in an array if your app supports multi windows, this is the time // when you should delete the corresponding element. mainWindow = null; }); }); 

TL DR;

Threads do not exist in Node.js, and an electron is a wireframe built on Node.js \ io.js and Chromium. Electronic Foundation Node.js. You can create child processes with forking in Node.js.

+8
source

Check out the Electron Edge. It allows you to run .NET code and node.js in one process (no IPC required, higher performance). This means that you can use a multi-threaded .NET model with single-thread node.js.

It will run on Linux, Mac, and Windows thanks to .NET Core.

https://github.com/kexplo/electron-edge

0
source

According to Multithreading docs:

With web workers, you can run JavaScript in OS-level threads.

All Node built-in modules are supported, however, none of the Electron built-in modules or built-in bindings should be used by Web Workers because they are not designed for streaming protection.

0
source

All Articles