Child(3pm)

πŸ“¦
version 0.013 Β· 5 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– Child β€” bestie's guide to fork() without the tears πŸ’–

ok so this is Child(3pm), straight out of the User Contributed Perl Documentation section, and honestly? it's a whole vibe. sit down, grab a strawberry drink, we're doing processes today πŸ“


✨ NAME ✨

Child β€” an object oriented, simple interface to fork() πŸ’…


🌸 DESCRIPTION 🌸

okay so real talk: fork() is lowkey a mess. it's too low level and it's genuinely hard to manage. the way that people just... forget to exit() at the end? forget to reap their children? forget to check exit status? 😭 every single time. and the problem is literally the low level functions you're given to do those things. throw pipes for IPC on top of that and you've got a whole pile of stuff that nobody on this earth wants to think about, fr fr.

Child is an Object Oriented interface to fork. it gives you a clean way to start a child process, and manage it afterwards. it's giving: methods for running, waiting, killing, checking, and even communicating with a child process. iconic behavior only πŸ‘‘

⚠️ NOTE: kill() is unpredictable on windows β€” strawberry perl sends the kill signal to the parent as well as the child. not a vibe. be careful out there bestie πŸ’€

⭐ SYNOPSIS ⭐

πŸŽ€ BASIC

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

πŸ¦‹ IPC

# 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");

🌈 SHORTCUT

Child can export the child() shortcut function when you ask for it. this function creates and starts the child process in one action. one step, no waiting, slay ✨

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;

πŸ’œ DETAILS πŸ’œ

first you define a child β€” you do this by constructing a Child object. and listen: defining a child does not start a new process. it's just the way to define what the new process will look like. think of it as the blueprint, not the build πŸ—οΈ

once you've defined the child, you can start the process by calling $child->start(). and here's the cute part β€” one child object can start as many processes as you like. infinite slay, no cap.

when you start a child, an Child::Link::Proc object is returned. this object provides multiple useful methods for interacting with your process. and within the process itself, an Child::Link::Parent is created and passed as the only parameter to the function you used to define the child. the parent object is how the child interacts with its parent 🫢


πŸ‘‘ PROCESS MANAGEMENT METHODS πŸ‘‘

  • @procs = Child->all_procs() β€” get a list of all the processes that have been started. heads up: this list is cleared in processes when they are started, that is, a child will not list its siblings. no snitching on the fam πŸ™ˆ
  • @pids = Child->all_proc_pids() β€” get a list of all the pids of processes that have been started.
  • Child->wait_all() β€” call wait() on all processes.

🧚 EXPORTS 🧚

  • $proc = child( sub { ... } )
  • $proc = child { ... }
  • $proc = child( sub { ... }, $plugin, @data )
  • $proc = child { ... } $plugin => @data

β€” create and start a process in one action. that's it, that's the whole job πŸ’…


πŸŽ€ CONSTRUCTOR πŸŽ€

  • $child = Child->new( sub { ... } )
  • $child = Child->new( sub { ... }, $plugin, @plugin_data )

β€” create a new Child object. does not start the child. you gotta call start for that, bestie.


✨ OBJECT METHODS ✨

  • $proc = $child->start() β€” start the child process. go go go πŸ”₯

🌸 SEE ALSO 🌸

  • Child::Link::Proc β€” the proc object that is returned by $child->start()
  • Child::Link::Parent β€” the parent object that is provided as the argument to the function used to define the child.
  • Child::Link::IPC β€” the base class for IPC plugin link objects. this provides the IPC methods.

πŸ’œ HISTORY πŸ’œ

most of this was part of Parallel::Runner, intended for use in the Fennec project. Fennec is being broken into multiple parts, and this is one such part. glow up era 🌱


πŸ¦‹ FENNEC PROJECT πŸ¦‹

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, and such tools get turned into their own projects. this is one such project.

  • Fennec - The core framework β€” the primary Fennec project that ties them all together.

⭐ AUTHORS ⭐

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.


okay that's the whole page bestie, go forth and fork responsibly πŸ«ΆπŸ’–

this was Child(3pm) as documented for perl v5.36.0, dated 2022-10-13 β€” periodt βœ¨πŸ‘‘

πŸ“¦ where this page comes from