child_process.fork() in Electron

我只是一个虾纸丫 提交于 2021-02-19 04:46:05

问题


Is it possible to fork a child_process from an electron render 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. This code works, when I run this with cmd and under node. But when I try to integrate it in my electron app, I can not communicate with the child.send() method.

// 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.

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. 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.


回答1:


Electron's renderer process is not the right place for forking child processes, you should think about moving this to the main process.

Nonetheless, it should work the way you describe. If you'd make a minimal example available somewhere I could take a closer look.



来源:https://stackoverflow.com/questions/40498183/child-process-fork-in-electron

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!