FAQ
I'd like to create fork (via child_process.fork) and make both master and
child exit after all tasks in master process are done.
When I do:

var child = child_process.fork("foo.js");

Process just hangs. I can of course exit it manually via `process.exit`,
but it's a generic module, and I don't have information when it's ok to do
so as there might be some other asynchronous tasks I'm not aware of.

Is there any way to be informed that all event loops for current process
have finished it's work? Something like process.lastTick, which would emit
callback if there's no other event loops in a queue ?

Thanks

--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Search Discussions

  • Jeff Kingyens at Jul 31, 2012 at 1:06 am
    Using child_process.fork enables bi-directional message passing between
    parent and child (see node.js documentation).
    While im not sure exactly what you are trying to do, this functionality
    should be all that you need.

    When the event loop in a node.js process is exhausted the process will exit
    causing 'exit' event on process object.
    You should handle this 'exit' event on the parent process and then send a
    message to the child process to clean itself up.
    If you need to do any cleanup on the parent after the child has closed then
    wait on a final message from the child saying
    its done. The details for you maybe be different, but this is the general
    idea.

    Jeff
    On Friday, July 27, 2012 9:00:19 AM UTC-7, Mariusz Nowak wrote:

    I'd like to create fork (via child_process.fork) and make both master and
    child exit after all tasks in master process are done.
    When I do:

    var child = child_process.fork("foo.js");

    Process just hangs. I can of course exit it manually via `process.exit`,
    but it's a generic module, and I don't have information when it's ok to do
    so as there might be some other asynchronous tasks I'm not aware of.

    Is there any way to be informed that all event loops for current process
    have finished it's work? Something like process.lastTick, which would emit
    callback if there's no other event loops in a queue ?

    Thanks
    --
    Job Board: http://jobs.nodejs.org/
    Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
    You received this message because you are subscribed to the Google
    Groups "nodejs" group.
    To post to this group, send email to nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Mariusz Nowak at Jul 31, 2012 at 7:21 am
    @Jeff If you're handling not detached child processes process will never
    'exit' naturally, so waiting for 'exit' event means waiting forever. You
    need to either manually kill child process or manually kill master process,
    and you just can't do it in generic low-level module.

    I already found out, that what I was after is currently impossible. I can
    start other process within Node.js in not persistent way using spawn and
    detached set to true, but it will only work if I don't setup any
    communication between current process and one I'm creating. There's no way
    to have both detached process and communicate with it via IPC channel.

    Anyway I decided that what I wanted to achieve is not
    clean, creating background processes in generic modules is not great, and I
    gone other path, less ideal from the functionality point of view, but
    definitely cleaner.

    On Tuesday, July 31, 2012 2:52:41 AM UTC+2, Jeff Kingyens wrote:

    Using child_process.fork enables bi-directional message passing between
    parent and child (see node.js documentation).
    While im not sure exactly what you are trying to do, this functionality
    should be all that you need.

    When the event loop in a node.js process is exhausted the process will
    exit causing 'exit' event on process object.
    You should handle this 'exit' event on the parent process and then send a
    message to the child process to clean itself up.
    If you need to do any cleanup on the parent after the child has closed
    then wait on a final message from the child saying
    its done. The details for you maybe be different, but this is the general
    idea.

    Jeff
    On Friday, July 27, 2012 9:00:19 AM UTC-7, Mariusz Nowak wrote:

    I'd like to create fork (via child_process.fork) and make both master and
    child exit after all tasks in master process are done.
    When I do:

    var child = child_process.fork("foo.js");

    Process just hangs. I can of course exit it manually via `process.exit`,
    but it's a generic module, and I don't have information when it's ok to do
    so as there might be some other asynchronous tasks I'm not aware of.

    Is there any way to be informed that all event loops for current process
    have finished it's work? Something like process.lastTick, which would emit
    callback if there's no other event loops in a queue ?

    Thanks
    --
    Job Board: http://jobs.nodejs.org/
    Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
    You received this message because you are subscribed to the Google
    Groups "nodejs" group.
    To post to this group, send email to nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Mariusz Nowak at Jul 31, 2012 at 7:53 am
    Accidentally I just found very related
    issue https://github.com/joyent/node/issues/2605
    On Tuesday, July 31, 2012 9:15:14 AM UTC+2, Mariusz Nowak wrote:

    @Jeff If you're handling not detached child processes process will never
    'exit' naturally, so waiting for 'exit' event means waiting forever. You
    need to either manually kill child process or manually kill master process,
    and you just can't do it in generic low-level module.

    I already found out, that what I was after is currently impossible. I can
    start other process within Node.js in not persistent way using spawn and
    detached set to true, but it will only work if I don't setup any
    communication between current process and one I'm creating. There's no way
    to have both detached process and communicate with it via IPC channel.

    Anyway I decided that what I wanted to achieve is not
    clean, creating background processes in generic modules is not great, and I
    gone other path, less ideal from the functionality point of view, but
    definitely cleaner.

    On Tuesday, July 31, 2012 2:52:41 AM UTC+2, Jeff Kingyens wrote:

    Using child_process.fork enables bi-directional message passing between
    parent and child (see node.js documentation).
    While im not sure exactly what you are trying to do, this functionality
    should be all that you need.

    When the event loop in a node.js process is exhausted the process will
    exit causing 'exit' event on process object.
    You should handle this 'exit' event on the parent process and then send a
    message to the child process to clean itself up.
    If you need to do any cleanup on the parent after the child has closed
    then wait on a final message from the child saying
    its done. The details for you maybe be different, but this is the general
    idea.

    Jeff
    On Friday, July 27, 2012 9:00:19 AM UTC-7, Mariusz Nowak wrote:

    I'd like to create fork (via child_process.fork) and make both master
    and child exit after all tasks in master process are done.
    When I do:

    var child = child_process.fork("foo.js");

    Process just hangs. I can of course exit it manually via `process.exit`,
    but it's a generic module, and I don't have information when it's ok to do
    so as there might be some other asynchronous tasks I'm not aware of.

    Is there any way to be informed that all event loops for current process
    have finished it's work? Something like process.lastTick, which would emit
    callback if there's no other event loops in a queue ?

    Thanks
    --
    Job Board: http://jobs.nodejs.org/
    Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
    You received this message because you are subscribed to the Google
    Groups "nodejs" group.
    To post to this group, send email to nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Mariusz Nowak at Jul 31, 2012 at 7:56 am
    and this: https://github.com/joyent/node/issues/3799 it looks this behavior
    may change in close future :)
    On Tuesday, July 31, 2012 9:53:01 AM UTC+2, Mariusz Nowak wrote:

    Accidentally I just found very related issue
    https://github.com/joyent/node/issues/2605
    On Tuesday, July 31, 2012 9:15:14 AM UTC+2, Mariusz Nowak wrote:

    @Jeff If you're handling not detached child processes process will never
    'exit' naturally, so waiting for 'exit' event means waiting forever. You
    need to either manually kill child process or manually kill master process,
    and you just can't do it in generic low-level module.

    I already found out, that what I was after is currently impossible. I can
    start other process within Node.js in not persistent way using spawn and
    detached set to true, but it will only work if I don't setup any
    communication between current process and one I'm creating. There's no way
    to have both detached process and communicate with it via IPC channel.

    Anyway I decided that what I wanted to achieve is not
    clean, creating background processes in generic modules is not great, and I
    gone other path, less ideal from the functionality point of view, but
    definitely cleaner.

    On Tuesday, July 31, 2012 2:52:41 AM UTC+2, Jeff Kingyens wrote:

    Using child_process.fork enables bi-directional message passing between
    parent and child (see node.js documentation).
    While im not sure exactly what you are trying to do, this functionality
    should be all that you need.

    When the event loop in a node.js process is exhausted the process will
    exit causing 'exit' event on process object.
    You should handle this 'exit' event on the parent process and then send
    a message to the child process to clean itself up.
    If you need to do any cleanup on the parent after the child has closed
    then wait on a final message from the child saying
    its done. The details for you maybe be different, but this is the
    general idea.

    Jeff
    On Friday, July 27, 2012 9:00:19 AM UTC-7, Mariusz Nowak wrote:

    I'd like to create fork (via child_process.fork) and make both master
    and child exit after all tasks in master process are done.
    When I do:

    var child = child_process.fork("foo.js");

    Process just hangs. I can of course exit it manually via
    `process.exit`, but it's a generic module, and I don't have information
    when it's ok to do so as there might be some other asynchronous tasks I'm
    not aware of.

    Is there any way to be informed that all event loops for current
    process have finished it's work? Something like process.lastTick, which
    would emit callback if there's no other event loops in a queue ?

    Thanks
    --
    Job Board: http://jobs.nodejs.org/
    Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
    You received this message because you are subscribed to the Google
    Groups "nodejs" group.
    To post to this group, send email to nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Alan Gutierrez at Jul 31, 2012 at 5:18 pm

    On Tue, Jul 31, 2012 at 12:15:14AM -0700, Mariusz Nowak wrote:
    @Jeff If you're handling not detached child processes process will never
    'exit' naturally, so waiting for 'exit' event means waiting forever. You
    need to either manually kill child process or manually kill master process,
    and you just can't do it in generic low-level module.

    I already found out, that what I was after is currently impossible. I can
    start other process within Node.js in not persistent way using spawn and
    detached set to true, but it will only work if I don't setup any
    communication between current process and one I'm creating. There's no way
    to have both detached process and communicate with it via IPC channel.
    Why not use named pipes?
    Anyway I decided that what I wanted to achieve is not clean, creating
    background processes in generic modules is not great, and I gone other path,
    less ideal from the functionality point of view, but definitely cleaner.
    An out-of-process architecture does not seem entirely unreasonable, actually.

    --
    Alan Gutierrez - http://twitter.com/bigeasy - http://github.com/bigeasy

    --
    Job Board: http://jobs.nodejs.org/
    Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
    You received this message because you are subscribed to the Google
    Groups "nodejs" group.
    To post to this group, send email to nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Mariusz Nowak at Jul 31, 2012 at 5:31 pm

    On Tuesday, July 31, 2012 7:18:20 PM UTC+2, Alan Gutierrez wrote:
    Why not use named pipes?
    @Alan sorry I'm not fluent at the server-side terminology, do
    child_process.fork does something different? Can you show a simple example
    how I can setup such communication in Node, without blocking exit of main
    process?

    --
    Mariusz Nowak
    http://github.com/medikoo
    http://twitter.com/medikoo

    Anyway I decided that what I wanted to achieve is not clean, creating
    background processes in generic modules is not great, and I gone other path,
    less ideal from the functionality point of view, but definitely cleaner.
    An out-of-process architecture does not seem entirely unreasonable,
    actually.

    --
    Alan Gutierrez - http://twitter.com/bigeasy - http://github.com/bigeasy
    --
    Job Board: http://jobs.nodejs.org/
    Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
    You received this message because you are subscribed to the Google
    Groups "nodejs" group.
    To post to this group, send email to nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Bert Belder at Aug 1, 2012 at 11:00 pm

    On Friday, July 27, 2012 6:00:19 PM UTC+2, Mariusz Nowak wrote:
    I'd like to create fork (via child_process.fork) and make both master and
    child exit after all tasks in master process are done

    If you're using node 0.8, you can do this:

    var cp = fork("foo.js")
    cp.unref();

    This will effectively "kill" the child process when the master exits, so
    you may want to install a SIGHUP handler in your child process.

    - Bert

    --
    Job Board: http://jobs.nodejs.org/
    Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
    You received this message because you are subscribed to the Google
    Groups "nodejs" group.
    To post to this group, send email to nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Mariusz Nowak at Aug 2, 2012 at 7:00 am
    @Bert What I want to achieve is to be able to setup communication bridge
    with other process, but without blocking exit of main process.

    Following code will keep process running forever:

    var child = require('child_process').fork('./child.js');
    child.on('message', function () {
    // Process message'
    });
    child.unref();

    It would be good to have some option to make it work similar way to the way
    watchers work with 'persistent' option set to false.
    Following code will exit immediately, even though we've initialized
    watcher, attached listener to it and didn't remove it:

    var watch = require('fs').watch(__dirname, { persistent: false }, function
    (event) {
    // Process event
    });

    I wonder if similar setup would be possible with process communication.

    --
    Mariusz Nowak
    http://github.com/medikoo
    http://twitter.com/medikoo


    On Thursday, August 2, 2012 12:59:54 AM UTC+2, Bert Belder wrote:
    On Friday, July 27, 2012 6:00:19 PM UTC+2, Mariusz Nowak wrote:

    I'd like to create fork (via child_process.fork) and make both master and
    child exit after all tasks in master process are done

    If you're using node 0.8, you can do this:

    var cp = fork("foo.js")
    cp.unref();

    This will effectively "kill" the child process when the master exits, so
    you may want to install a SIGHUP handler in your child process.

    - Bert
    --
    Job Board: http://jobs.nodejs.org/
    Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
    You received this message because you are subscribed to the Google
    Groups "nodejs" group.
    To post to this group, send email to nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupnodejs @
categoriesnodejs
postedJul 27, '12 at 4:00p
activeAug 2, '12 at 7:00a
posts9
users4
websitenodejs.org
irc#node.js

People

Translate

site design / logo © 2022 Grokbase