简体   繁体   English

电子中的child_process.fork()

[英]child_process.fork() in Electron

Is it possible to fork a child_process from an electron render process? 是否可以从电子渲染过程中分叉child_process? I found some posts across the net, but there were no hint how helps me to get my code working. 我在网上发现了一些帖子,但没有提示如何让我的代码正常工作。 I created a module, that fork child processes. 我创建了一个模块,即fork子进程。 This code works, when I run this with cmd and under node. 当我使用cmd和节点下运行时,此代码可以正常工作。 But when I try to integrate it in my electron app, I can not communicate with the child.send() method. 但是当我尝试将它集成到我的电子应用程序中时,我无法与child.send()方法进行通信。

// create fork
const fork = require('child_process').fork;
const fs = require('fs');

const img_path = [
'path/to/an/image1.jpg',
'path/to/an/image2.jpg',
'path/to/an/image3.jpg'
];

const cp = [];

const temp_path = img_path.map((item) => item);

createAndResize(2);

function createAndResize(num) {
    return childResize(createChildProcess(num));
}

function createChildProcess(num) {
    if(num <= 0) {
        return cp;
    } else {
        let cf = fork('./child.js');
        cp.push(cf);
        num -= 1;
        return createChildProcess(num);
    }
}

function childResize(list) {
    if(list.length <=0) {
        return true;
    } else {
     // child_process is created
        let child = list.shift();

         child.on('message', function (data) { 
                if (!temp_path.length) {
                    process.kill(data);
                } else {
                    child.send(temp_path.shift());  
                }
            });

            child.send(temp_path.shift());   

            setTimeout(function() {
                childResize(list);
            }, 1000);      
    }
}

//child.js
process.on('message', function(msg) {
console.log(msg); //this is never reached
};

EDIT: based on the comment below, I fork child processes on the main process. 编辑:基于下面的评论,我在主进程上分叉子进程。 The comunication seems to work with few exceptions. 通信似乎有一些例外。 But first my new code: 但首先我的新代码:

    // myView.js
    const { remote } = require('electron');
    const mainProcess = remote.require('./main.js');
    const { forkChildfromMain } = mainProcess;

    forkChildfromMain();


    // main.js
        const fork = require('child_process').fork;
        let cp = [];



function forkChildfromMain() { 
    createAndResize(4);
}

function createAndResize(num) {
        return childResize(createChildProcess(num));
    }
    function createChildProcess(num) {
        if(num <= 0) {
            return cp;
        } else {
            let cf = fork('./resize.js');
            cp.push(cf);
            num -= 1;
            return createChildProcess(num);
        }
    }

    function childResize(list) {
        if(list.length <=0) {
            return true;
        } else {
            let child = list.shift();

             child.on('message', function (msg) {
    // logs 'Hello World' to the cmd console
    console.log(msg);
    });
                child.send('Hello World');   
                setTimeout(function() {
                    childResize(list);
                }, 1000);      
        }
    }

exports.forkChildfromMain = forkChildfromMain;


    // child.js
    process.on('message', function(msg) {
    // this console statement get never loged
    // I think, I must integrate an icpModule
        console.log(msg);

    //process send msg back to main.js
    process.send(msg);
    })

OUTDATED: The main problem now is, that I think electron 'spawn' new child processes and do not fork. 陈述:现在的主要问题是,我认为电子'生成'新的子进程并且不进行分叉。 Because, when I look at my task manager I see only one instance from electron. 因为,当我看到我的任务管理器时,我只看到一个来自电子的实例。 When I run the code in a node env, I see there were fork multiple node instances. 当我在节点env中运行代码时,我看到有多个fork节点实例。

The reason why I prefer to fork my child processes in multiple node instances is, that I want to make many image manipulation. 我更喜欢在多个节点实例中分叉我的子进程的原因是,我想要进行许多图像处理。 So when I fork childs, then every child has it own node instance with memory and so on. 因此,当我分叉子节点时,每个子节点都拥有自己的带有内存的节点实例,依此类推。 I think that would be more performant then when I only have one instance who shared the memory and resources to all of the childs. 我认为当我只有一个为所有孩子分享内存和资源的实例时,这会更有效。

The second unexpected behavior is, that the console.log statement in the child is not printed to my cmd console. 第二个意外的行为是,子项中的console.log语句未打印到我的cmd控制台。 But this is the smaller ones :) 但这是较小的:)

EDIT: After I analyse my task manager a little more in depth, I saw, that electron spawn multiple child processes like it should. 编辑:在我深入分析我的任务管理器之后,我看到,电子会产生多个类似于它的子进程。

Electron's renderer process is not the right place for forking child processes, you should think about moving this to the main process. Electron的渲染器过程不是分配子进程的正确位置,您应该考虑将其移动到主进程。

Nonetheless, it should work the way you describe. 尽管如此,它应该按照你描述的方式工作。 If you'd make a minimal example available somewhere I could take a closer look. 如果你在某个地方提供一个最小的例子,我可以仔细看看。

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

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