Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Electron中的child_process.fork()_Node.js_Electron - Fatal编程技术网

Node.js Electron中的child_process.fork()

Node.js Electron中的child_process.fork(),node.js,electron,Node.js,Electron,有可能从电子渲染过程中派生出子_过程吗?我在网上找到了一些帖子,但是没有提示如何帮助我的代码工作。 我创建了一个模块,用于分叉子进程。当我使用cmd和node运行此代码时,此代码有效。但是当我尝试将它集成到我的电子应用程序中时,我无法与child.send()方法通信 //创建fork const fork=require('child_process')。fork; 常数fs=要求('fs'); 常量img_路径=[ 'path/to/an/image1.jpg', 'path/to/an/i

有可能从电子渲染过程中派生出子_过程吗?我在网上找到了一些帖子,但是没有提示如何帮助我的代码工作。 我创建了一个模块,用于分叉子进程。当我使用cmd和node运行此代码时,此代码有效。但是当我尝试将它集成到我的电子应用程序中时,我无法与child.send()方法通信

//创建fork
const fork=require('child_process')。fork;
常数fs=要求('fs');
常量img_路径=[
'path/to/an/image1.jpg',
'path/to/an/image2.jpg',
'path/to/an/image3.jpg'
];
常数cp=[];
const temp_path=img_path.map((项目)=>项目);
大小(2);
函数createAndResize(num){
返回childResize(createChildProcess(num));
}
函数createChildProcess(num){

如果(numElectron的渲染器进程不是分叉子进程的正确位置,那么应该考虑将其移动到主进程


尽管如此,它应该按照您所描述的方式工作。如果您能在某个地方提供一个最小的示例,我可以仔细看看。

您能解释一下为什么渲染过程不是派生子进程的地方吗?我也尝试过在主进程中派生子进程。但这是相同的问题。我将再次尝试,并很快通过一个新代码。之后我意识到我不能从孩子那里记录msg,我再次尝试了我的第一个代码,它成功了。但我仍然对你的陈述感兴趣,为什么你认为渲染进程不是创建子进程的正确位置。@Zantinger,渲染进程是你的UI代码所在的地方,对于许多模块来说,让它们存在于子进程中是有意义的渲染器进程,即使(在客户端/服务器应用程序中,您可以将它们放在服务器上)。由于使用了Chrome DevTools,调试渲染器进程变得非常容易。但是,对于依赖于操作系统的功能(如分叉)把它放在主进程中通常是有意义的。我的应用程序的主进程中通常没有太多代码,但就像我提到的依赖于我放在那里的操作系统的调用一样,我从不后悔这个选择。但实际上,对于小应用程序来说,让主进程尽可能简单可能是有意义的。
// 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
};
    // 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);
    })