Javascript electron框架是否允许通过web workers执行多线程?

Javascript electron框架是否允许通过web workers执行多线程?,javascript,electron,Javascript,Electron,如果我制作了一个电子应用程序,我很难在谷歌上搜索我将如何实现多线程。会不会是网络工作者?在Electron、Node.js和Process上 电子的工作原理与节点相同。在Node.js中,线程不是执行的原子单元。您在事件循环中工作,默认情况下执行是异步的。看见也就是说,您可以通过分叉来加速Node.js中的多个子进程 这里有一个例子 //forking a process using cluster var cluster = require('cluster'); var http = req

如果我制作了一个电子应用程序,我很难在谷歌上搜索我将如何实现多线程。会不会是网络工作者?

在Electron、Node.js和Process上 电子的工作原理与节点相同。在Node.js中,线程不是执行的原子单元。您在事件循环中工作,默认情况下执行是异步的。看见也就是说,您可以通过分叉来加速Node.js中的多个子进程

这里有一个例子

//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);
}
TL;博士:


Node.js中不存在线程,Electron是基于Node.js\io.js和Chromium构建的框架。电子的基础是Node.js。您可以在Node.js中使用forking生成子进程。

在您可以创建的渲染器进程中,这些子进程将在它们自己的线程中运行,但是,因为节点不是线程安全的。因此,如果您想在使用Node的单独线程中运行某些内容,那么您需要生成一个单独的进程,您可以这样做,然后使用与新进程通信。

请查看Electron Edge。它使您能够在单个进程中运行.NET代码和node.js(无需IPC,性能更佳)。这意味着您可以利用.NET的多线程模型和node.js的单线程性

多亏了.NET内核,它将在Linux、Mac和Windows上运行

根据文件:

使用Web Workers,可以在操作系统级线程中运行JavaScript


支持节点的所有内置模块,但Electron的内置模块或具有本机绑定的模块均不应在Web Workers中使用,因为它们不是为线程安全而设计的。

您可以在主进程中尝试以下内容:。这可能比多节点进程更易于管理。你的使用案例是什么?与其改变你的核心问题,你可以只添加一条你不懂的注释。或者只打开一个不可见的BrowserWindow,它运行在不同的进程中,你可以与其他BrowserWindow进行IPC通信将.net添加到electron只用于多处理是疯狂的。。还有其他几种可能性,比如打开不需要技术突破的背景窗口我不会说“疯狂”…对于已经使用过的人来说听起来很有趣。netalso需要windows我不这么认为。net core在centos和其他一些Linux发行版上运行。如果您有一些需要在其上执行的c#库或代码,我认为这非常有用。更干净,然后用bat或cscript包装,然后用execShell调用。谢谢。请查看此文件,了解有关生成子进程的各种方法的更多信息。我可能会错,因为我不是节点专家,但我认为for循环中的fork将为您提供2^n-1个子进程,而不是n个进程
'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;
  });
});