简体   繁体   English

将主进程作为child_process运行

[英]Run main process as child_process

I am having troubles with getting child_process working with Atom Electron. 我在使用Atom Electron处理child_process时遇到了麻烦。 First of all, I am using the pre-compiled binary app that you can download from Electron's website: 首先,我使用的是预编译的二进制应用程序,您可以从Electron的网站下载:

  • I am using the usual pre-compiled binary on Mac OS X. 我在Mac OS X上使用通常的预编译二进制文件。
  • In myapp.app/Contents/Resources I made a folder app as described. myapp.app/Contents/Resources我按照描述myapp.app/Contents/Resources了一个文件夹应用程序。
  • I added a brief package.json inside it, setting index.js as main script. 我在其中添加了一个简短的package.json ,将index.js设置为主脚本。

Now, if I add to index.js the following snippet: 现在,如果我向index.js添加以下代码段:

'use strict';

var electron = require('electron');
var app = electron.app;

const BrowserWindow = electron.BrowserWindow;

var mainWindow;

function createWindow () {
  mainWindow = new BrowserWindow({width: 800, height: 600});
  mainWindow.loadURL('file://' + __dirname + '/index.html');
  mainWindow.webContents.openDevTools();
  mainWindow.on('closed', function()
  {
    mainWindow = null;
  });
}

app.on('ready', createWindow);

app.on('window-all-closed', function () {
  if (process.platform !== 'darwin')
  {
    app.quit();
  }
});

app.on('activate', function () {
  if (mainWindow === null)
  {
    createWindow();
  }
});

(that is basically the example code to get things working) everything works fine. (这基本上是使事情正常工作的示例代码)一切正常。 I get the window and I can get it to do basically anything. 我得到了窗口,我可以基本上做任何事情。

Now, for reasons related to updates, I am in need to slightly change this paradigm. 现在,由于与更新相关的原因,我需要稍微改变这个范例。 What I would need is to be able to perform several tasks from index.js without any need to do any gui operation (it should be some sort of daemon) and then to start some child.js script as a child_process from index.js . 我需要的是能够从index.js执行几个任务而不需要做任何gui操作 (它应该是某种守护进程)然后从index.js启动一些child.js脚本作为child_process child.js should be able to open windows and all the rest. child.js应该能够打开窗户和其他所有。

So here was my naive try. 所以这是我天真的尝试。 I just cut and pasted the example snippet above in child.js , then edited index.js into the following: 我只是将上面的示例代码段剪切并粘贴到child.js ,然后将index.js编辑成以下内容:

var child_process = require('child_process');
var my_child = child_process.fork(__dirname + '/child.js');

Quite minimal, right? 相当小,对吗? Hoped it would work, but it didn't. 希望它会起作用,但事实并非如此。 When I double click on my pretty app, nothing happens. 当我双击我漂亮的应用程序时,没有任何反应。 I bet I am doing something wrong in a trivial way, but I wouldn't be able to tell what. 我敢打赌,我在做一些不正确的事情,但我无法说出什么。

Update 1 I moved this out of my package so that I could get console.log s. 更新1我将其移出我的包,以便我可以获得console.log child.js dies with an error at require('electron') : it doesn't seem to be able to find it. child.jsrequire('electron')错误而死:它似乎无法找到它。

Update 2 : I listed the environment variables in child.js and noticed a ATOM_SHELL_INTERNAL_RUN_AS_NODE = 1 . 更新2 :我在child.js列出了环境变量,并注意到ATOM_SHELL_INTERNAL_RUN_AS_NODE = 1 I thought I should turn that to 0 , but nothing changed. 我以为我应该把它变成0 ,但没有改变。

In Electron child_process.fork() will spawn the child with the ATOM_SHELL_INTERNAL_RUN_AS_NODE environment variable set, which means that no Chromium features will be available in the child process (so no Electron built-in modules), all you'll get is the vanilla Node runtime plus a patched fs that can read inside ASAR files. 在Electron中, child_process.fork()将使用ATOM_SHELL_INTERNAL_RUN_AS_NODE环境变量集生成子项,这意味着子进程中没有Chromium功能可用(因此没有Electron内置模块),所有你得到的就是vanilla节点运行时加上可以在ASAR文件中读取的修补过的fs If you look at the code for child_process.fork() you'll see there's no way to override the value of ATOM_SHELL_INTERNAL_RUN_AS_NODE . 如果查看child_process.fork()的代码,您将看到无法覆盖ATOM_SHELL_INTERNAL_RUN_AS_NODE的值。 If you can, use child_process.spawn() instead, failing that I guess you could try opening an issue and asking if the Electron team will be open to modifying the current behavior. 如果可以的话,请使用child_process.spawn() ,如果失败,我猜你可以尝试打开一个问题并询问Electron团队是否会开放修改当前的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM