Child(3pm) | User Contributed Perl Documentation | Child(3pm) |
Child - Object oriented simple interface to fork()
Fork is too low level, and difficult to manage. Often people forget to exit at the end, reap their children, and check exit status. The problem is the low level functions provided to do these things. Throw in pipes for IPC and you just have a pile of things nobody wants to think about.
Child is an Object Oriented interface to fork. It provides a clean way to start a child process, and manage it afterwords. It provides methods for running, waiting, killing, checking, and even communicating with a child process.
NOTE: kill() is unpredictable on windows, strawberry perl sends the kill signal to the parent as well as the child.
use Child; my $child = Child->new(sub { my ( $parent ) = @_; .... # exit() is called for you at the end. }); my $proc = $child->start; # Kill the child if it is not done $proc->is_complete || $proc->kill(9); $proc->wait; #blocking
# Build with IPC my $child2 = Child->new(sub { my $self = shift; $self->say("message1"); $self->say("message2"); my $reply = $self->read(1); }, pipe => 1 ); my $proc2 = $child2->start; # Read (blocking) my $message1 = $proc2->read(); my $message2 = $proc2->read(); $proc2->say("reply");
Child can export the child() shortcut function when requested. This function creates and starts the child process in one action.
use Child qw/child/; my $proc = child { my $parent = shift; ... };
You can also request IPC:
use Child qw/child/; my $child = child { my $parent = shift; ... } pipe => 1;
First you define a child, you do this by constructing a Child object. Defining a child does not start a new process, it is just the way to define what the new process will look like. Once you have defined the child you can start the process by calling $child->start(). One child object can start as many processes as you like.
When you start a child an Child::Link::Proc object is returned. This object provides multiple useful methods for interacting with your process. Within the process itself an Child::Link::Parent is created and passed as the only parameter to the function used to define the child. The parent object is how the child interacts with its parent.
Most of this was part of Parallel::Runner intended for use in the Fennec project. Fennec is being broken into multiple parts, this is one such part.
This module is part of the Fennec project. See Fennec for more details. Fennec is a project to develop an extendable and powerful testing framework. Together the tools that make up the Fennec framework provide a potent testing environment.
The tools provided by Fennec are also useful on their own. Sometimes a tool created for Fennec is useful outside the greater framework. Such tools are turned into their own projects. This is one such project.
Chad Granum exodist7@gmail.com
Copyright (C) 2010 Chad Granum
Child is free software; Standard perl licence.
Child is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
2021-01-05 | perl v5.32.0 |