Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

child_process.fork() workaround for windows #1516

Closed
kraag22 opened this issue Jan 28, 2014 · 8 comments
Closed

child_process.fork() workaround for windows #1516

kraag22 opened this issue Jan 28, 2014 · 8 comments

Comments

@kraag22
Copy link

kraag22 commented Jan 28, 2014

If you want to use child_process.fork() you cannot, because of issue #213
But if specify your own node executable in the options like this
var processing = ch.fork('c:\\reader.js', [], {execPath: 'c:\\node.exe'})
you can use it. There is no problem running on mac, but in windows you forked node will crash if there is any stdio operation. Child process will crash with exit code 8.

So example code:

var ch = require('child_process')

console.log("I'm alive");

var processing = ch.fork('c:\\reader.js', [], {execPath: 'c:\\node.exe'})

processing.on('message', function(data) {
    console.log(data);
    if (data['status'] === 'ok') {
        console.log('ok');
    } else if (data['status'] === 'error') {
        console.log('error');
    }
});
processing.on('exit', function(code) {
    console.log('exited with code:', code);
});
processing.send({});

This code will fail on windows when the reader.js prints anything in the console for us. The workaround is adding silent:true to fork() options.

var processing = ch.fork('c:\\reader.js', [], {execPath: 'c:\\node.exe', silent: true})

I hope this will save some time to anyone experiencing this problem.

@rogerwang
Copy link
Member

Thanks for sharing.

@shabeepk
Copy link

I am very new to node, and here's my code. My code is crashing with exit code 8 for some reason that I can't figure out.

Can you please help?

I am using Windows, right now.

processor.js:

var ch = require('child_process')
var path = require('path');

var nodePath = path.join(path.dirname(process.execPath), 'node');
if (process.platform === 'win32') {
    nodePath += ".exe";
}

console.log("Spawning a new node process: " + nodePath);

var processing = ch.fork('js/processor_fork.js', [], {execPath: nodePath, silent: true});

processing.on('message', function(data) {
    console.log(data);
});

processing.on('exit', function(code) {
    console.log('exited with code:', code);
});

processing.send({});

processor_fork.js:

importScripts('js/somescript.js');
var timer;

function processNext(current, max) {
    someFunction();

    if ((current + 1) % 1000 == 0)
    {
        process.send({status: "Processed " + (current + 1)});
    }

    if (current < max)
        timer = setTimeout(processNext(current + 1, max), 10);
}

process.on('message', function(data) {
    if (timer)
        clearTimeout(timer);

    timer = setTimeout(processNext(0, 20000), 10);
});

@shabeepk
Copy link

@rogerwang Do you know what I am doing wrong in the above code? Can I do this some other way?

@laike9m
Copy link

laike9m commented Jul 28, 2014

@kraag22 You saved my life, thank you so much!

@BoneLee
Copy link

BoneLee commented Aug 31, 2014

What about Linux? fork doesn't work on Linux either.

@rogerwang
Copy link
Member

@BoneLee fork has been fixed in git and will be released soon with 0.10.3. You can try it now with live build, see the links in #213

@JoyKrishnaMondal
Copy link

Thanks a lot !! This is amazing

@nobook
Copy link

nobook commented Dec 15, 2016

Thanks a lot !!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants