How to fork child in "non persistent" way?

651 views
Skip to first unread message

Mariusz Nowak

unread,
Jul 27, 2012, 12:00:19 PM7/27/12
to nod...@googlegroups.com
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

Mariusz Nowak

unread,
Jul 31, 2012, 3:15:14 AM7/31/12
to nod...@googlegroups.com
@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

Mariusz Nowak

unread,
Jul 31, 2012, 3:53:01 AM7/31/12
to nod...@googlegroups.com
Accidentally I just found very related issue https://github.com/joyent/node/issues/2605

Mariusz Nowak

unread,
Jul 31, 2012, 3:56:22 AM7/31/12
to nod...@googlegroups.com
and this: https://github.com/joyent/node/issues/3799 it looks this behavior may change in close future :)

Alan Gutierrez

unread,
Jul 31, 2012, 1:18:20 PM7/31/12
to nod...@googlegroups.com
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

Mariusz Nowak

unread,
Jul 31, 2012, 1:31:42 PM7/31/12
to nod...@googlegroups.com


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

Bert Belder

unread,
Aug 1, 2012, 6:59:54 PM8/1/12
to nod...@googlegroups.com
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

Mariusz Nowak

unread,
Aug 2, 2012, 3:00:52 AM8/2/12
to nod...@googlegroups.com
@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.
Reply all
Reply to author
Forward
0 new messages