1Child(3) User Contributed Perl Documentation Child(3)
2
3
4
6 Child - Object oriented simple interface to fork()
7
9 Fork is too low level, and difficult to manage. Often people forget to
10 exit at the end, reap their children, and check exit status. The
11 problem is the low level functions provided to do these things. Throw
12 in pipes for IPC and you just have a pile of things nobody wants to
13 think about.
14
15 Child is an Object Oriented interface to fork. It provides a clean way
16 to start a child process, and manage it afterwords. It provides methods
17 for running, waiting, killing, checking, and even communicating with a
18 child process.
19
20 NOTE: kill() is unpredictable on windows, strawberry perl sends the
21 kill signal to the parent as well as the child.
22
24 BASIC
25 use Child;
26
27 my $child = Child->new(sub {
28 my ( $parent ) = @_;
29 ....
30 # exit() is called for you at the end.
31 });
32 my $proc = $child->start;
33
34 # Kill the child if it is not done
35 $proc->is_complete || $proc->kill(9);
36
37 $proc->wait; #blocking
38
39 IPC
40 # Build with IPC
41 my $child2 = Child->new(sub {
42 my $self = shift;
43 $self->say("message1");
44 $self->say("message2");
45 my $reply = $self->read(1);
46 }, pipe => 1 );
47 my $proc2 = $child2->start;
48
49 # Read (blocking)
50 my $message1 = $proc2->read();
51 my $message2 = $proc2->read();
52
53 $proc2->say("reply");
54
55 SHORTCUT
56 Child can export the child() shortcut function when requested. This
57 function creates and starts the child process in one action.
58
59 use Child qw/child/;
60
61 my $proc = child {
62 my $parent = shift;
63 ...
64 };
65
66 You can also request IPC:
67
68 use Child qw/child/;
69
70 my $child = child {
71 my $parent = shift;
72 ...
73 } pipe => 1;
74
76 First you define a child, you do this by constructing a Child object.
77 Defining a child does not start a new process, it is just the way to
78 define what the new process will look like. Once you have defined the
79 child you can start the process by calling $child->start(). One child
80 object can start as many processes as you like.
81
82 When you start a child an Child::Link::Proc object is returned. This
83 object provides multiple useful methods for interacting with your
84 process. Within the process itself an Child::Link::Parent is created
85 and passed as the only parameter to the function used to define the
86 child. The parent object is how the child interacts with its parent.
87
89 @procs = Child->all_procs()
90 Get a list of all the processes that have been started. This list
91 is cleared in processes when they are started; that is a child will
92 not list its siblings.
93
94 @pids = Child->all_proc_pids()
95 Get a list of all the pids of processes that have been started.
96
97 Child->wait_all()
98 Call wait() on all processes.
99
101 $proc = child( sub { ... } )
102 $proc = child { ... }
103 $proc = child( sub { ... }, $plugin, @data )
104 $proc = child { ... } $plugin => @data
105 Create and start a process in one action.
106
108 $child = Child->new( sub { ... } )
109 $child = Child->new( sub { ... }, $plugin, @plugin_data )
110 Create a new Child object. Does not start the child.
111
113 $proc = $child->start()
114 Start the child process.
115
117 Child::Link::Proc
118 The proc object that is returned by $child->start()
119
120 Child::Link::Parent
121 The parent object that is provided as the argument to the function
122 used to define the child.
123
124 Child::Link::IPC
125 The base class for IPC plugin link objects. This provides the IPC
126 methods.
127
129 Most of this was part of Parallel::Runner intended for use in the
130 Fennec project. Fennec is being broken into multiple parts, this is one
131 such part.
132
134 This module is part of the Fennec project. See Fennec for more details.
135 Fennec is a project to develop an extendable and powerful testing
136 framework. Together the tools that make up the Fennec framework
137 provide a potent testing environment.
138
139 The tools provided by Fennec are also useful on their own. Sometimes a
140 tool created for Fennec is useful outside the greater framework. Such
141 tools are turned into their own projects. This is one such project.
142
143 Fennec - The core framework
144 The primary Fennec project that ties them all together.
145
147 Chad Granum exodist7@gmail.com
148
150 Copyright (C) 2010 Chad Granum
151
152 Child is free software; Standard perl licence.
153
154 Child is distributed in the hope that it will be useful, but WITHOUT
155 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
156 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
157
158
159
160perl v5.36.0 2022-07-22 Child(3)