NAME
Venus::Process - Process Class
ABSTRACT
Process Class for Perl 5
SYNOPSIS
package main;
use Venus::Process;
my $parent = Venus::Process->new;
my $process = $parent->fork;
if ($process) {
# do something in child process ...
$process->exit;
}
else {
# do something in parent process ...
$parent->wait(-1);
}
# $parent->exit;
DESCRIPTION
This package provides methods for handling and forking processes.
ATTRIBUTES
This package has the following attributes:
alarm
alarm(Int $seconds) (Int)
The alarm attribute is used in calls to alarm when the process is forked, installing an alarm in the forked process if set.
Since 2.40
INHERITS
This package inherits behaviors from:
INTEGRATES
This package integrates behaviors from:
METHODS
This package provides the following methods:
async
async(CodeRef $code, Any @args) (Process)
The async method creates a new Venus::Process object and asynchronously runs the callback provided via the "work" method. Both process objects are configured to be are dyadic, i.e. representing an exclusing bi-directoral relationship. Additionally, the callback return value will be automatically made available via the "await" method unless it's undefined. This method returns the newly created "dyadic" process object.
Since 3.40
- async example 1
-
# given: synopsis; my $async = $parent->async(sub{ my ($process) = @_; # in forked process ... $process->exit; }); # bless({...}, 'Venus::Process')
await
await(Int $timeout) (Any)
The await method expects to operate on a "dyadic" process object and blocks the execution of the current process until a value is received from its couterpart. If a timeout is provided, execution will be blocked until a value is received or the wait time expires. If a timeout of 0
is provided, execution will not be blocked. If no timeout is provided at all, execution will block indefinitely.
Since 3.40
- await example 1
-
# given: synopsis; my $async = $parent->async(sub{ ($process) = @_; # in forked process ... return 'done'; }); my $await = $async->await; # ['done']
- await example 2
-
# given: synopsis; my $async = $parent->async(sub{ ($process) = @_; # in forked process ... return {status => 'done'}; }); my $await = $async->await; # [{status => 'done'}]
- await example 3
-
# given: synopsis; my $async = $parent->async(sub{ ($process) = @_; # in forked process ... return 'done'; }); my ($await) = $async->await; # 'done'
- await example 4
-
# given: synopsis; my $async = $parent->async(sub{ ($process) = @_; # in forked process ... return {status => 'done'}; }); my ($await) = $async->await; # {status => 'done'}
- await example 5
-
# given: synopsis; my $async = $parent->async(sub{ ($process) = @_; # in forked process ... $process->sendall('send 1'); $process->sendall('send 2'); $process->sendall('send 3'); return; }); my $await; my $results = []; push @$results, $async->await; # 'send 1' push @$results, $async->await; # 'send 2' push @$results, $async->await; # 'send 3' $results; # ['send 1', 'send 2', 'send 3']
chdir
chdir(Str $path) (Process)
The chdir method changes the working directory the current process is operating within.
Since 0.06
- chdir example 2
-
# given: synopsis; $parent = $parent->chdir('/tmp'); # bless({...}, 'Venus::Process')
- chdir example 3
-
# given: synopsis; $parent = $parent->chdir('/xyz'); # Exception! (isa Venus::Process::Error) (see error_on_chdir)
check
check(Int $pid) (Int, Int)
The check method does a non-blocking "waitpid" in perlfunc operation and returns the wait status. In list context, returns the specified process' exit code (if terminated).
Since 0.06
- check example 1
-
package main; use Venus::Process; my $parent = Venus::Process->new; my ($process, $pid) = $parent->fork; if ($process) { # in forked process ... $process->exit; } my $check = $parent->check($pid); # 0
- check example 2
-
package main; use Venus::Process; my $parent = Venus::Process->new; my ($process, $pid) = $parent->fork; if ($process) { # in forked process ... $process->exit; } my ($check, $status) = $parent->check('00000'); # (-1, -1)
- check example 3
-
package main; use Venus::Process; my $parent = Venus::Process->new; my ($process, $pid) = $parent->fork(sub{ $_->exit(1) }); if ($process) { # in forked process ... $process->exit; } my ($check, $status) = $parent->check($pid); # ($pid, 1)
count
count(Str | CodeRef $code, Any @args) (Int)
The count method dispatches to the method specified (or the "watchlist" if not specified) and returns a count of the items returned from the dispatched call.
Since 2.40
- count example 1
-
package main; use Venus::Process; my $parent = Venus::Process->new; my $count = $parent->count; # 0
- count example 2
-
package main; use Venus::Process; my $parent = Venus::Process->new; my ($pid) = $parent->watch(1001); my $count = $parent->count; # 1
- count example 3
-
package main; use Venus::Process; my $parent = Venus::Process->new; my ($pid) = $parent->watch(1001); my $count = $parent->count('watchlist'); # 1
daemon
daemon() (Process)
The daemon method detaches the process from controlling terminal and runs it in the background as system daemon. This method internally calls "disengage" and "setsid" and attempts to change the working directory to the root directory.
Since 0.06
- daemon example 1
-
# given: synopsis; my $daemon = $parent->daemon; # exits parent immediately # in forked process ... # $daemon->exit;
data
data(Int @pids) (Int)
The data method returns the number of messages sent to the current process, from the PID or PIDs provided (if any). If no PID list is provided, the count returned is based on the PIDs returned from "watchlist".
Since 2.91
- data example 2
-
# given: synopsis package main; # in process 1 my $process_1 = Venus::Process->new(12345)->join('procs'); # in process 2 my $process_2 = Venus::Process->new(12346)->join('procs'); # in process 3 my $process_3 = Venus::Process->new(12347)->join('procs'); # in process 1 $process_1->pool(2)->sendall({ from => $process_1->pid, said => 'hello', }); # in process 2 $process_2->pool(2)->sendall({ from => $process_2->pid, said => 'hello', }); # $process_2->data; # 2 # in process 3 $process_3->pool(2)->sendall({ from => $process_3->pid, said => 'hello', }); # $process_3->data; # 2 # in process 1 my $data = $process_1->data; # 2
- data example 3
-
# given: synopsis package main; # in process 1 my $process_1 = Venus::Process->new(12345)->join('procs'); # in process 2 my $process_2 = Venus::Process->new(12346)->join('procs'); # in process 3 my $process_3 = Venus::Process->new(12347)->join('procs'); # in process 1 $process_1->pool(2)->sendall({ from => $process_1->pid, said => 'hello', }); # in process 2 $process_2->pool(2)->sendall({ from => $process_2->pid, said => 'hello', }); # $process_2->data; # 2 # in process 3 $process_3->pool(2)->sendall({ from => $process_3->pid, said => 'hello', }); # $process_3->data; # 2 # in process 1 $process_1->recvall; my $data = $process_1->data; # 0
decode
decode(Str $data) (Any)
The decode method accepts a string representation of a Perl value and returns the Perl value.
Since 2.91
- decode example 1
-
# given: synopsis package main; my $decode = $parent->decode("{ok=>1}"); # { ok => 1 }
disengage
disengage() (Process)
The disengage method limits the interactivity of the process by changing the working directory to the root directory and redirecting its standard file descriptors from and to /dev/null
, or the OS' equivalent. These state changes can be undone by calling the "engage" method.
Since 0.06
- disengage example 1
-
# given: synopsis; $parent = $parent->disengage; # bless({...}, 'Venus::Process')
encode
encode(Any $data) (Str)
The encode method accepts a Perl value and returns a string representation of that Perl value.
Since 2.91
engage
engage() (Process)
The engage method ensures the interactivity of the process by changing the working directory to the directory used to launch the process, and by redirecting/returning its standard file descriptors from and to their defaults. This method effectively does the opposite of the "disengage" method.
Since 0.06
exchange
exchange(Str $name) (Any)
The exchange method gets and/or sets the name of the data exchange. The exchange is the ontext in which processes can register and cooperate. Process can cooperate in different exchanges (or contexts) and messages sent to a process in one context are not available to be retrieved will operating in another exchange (or context).
Since 2.91
- exchange example 2
-
# given: synopsis package main; my $exchange = $parent->exchange('procs'); # "procs"
- exchange example 3
-
# given: synopsis package main; my $exchange = $parent->exchange('procs'); # "procs" $exchange = $parent->exchange; # "procs"
exit
exit(Int $status) (Int)
The exit method exits the program immediately.
Since 0.06
followers
followers() (ArrayRef)
The followers method returns the list of PIDs registered under the current "exchange" who are not the "leader".
Since 2.91
- followers example 2
-
# given: synopsis package main; # in process 1 my $process_1 = Venus::Process->new(12345)->register; # in process 2 my $process_2 = Venus::Process->new(12346)->register; # in process 3 my $process_3 = Venus::Process->new(12347)->register; # in process 1 my $followers = $process_1->followers; # [12346, 12347]
fork
fork(Str | CodeRef $code, Any @args) (Process, Int)
The fork method calls the system "fork" in perlfunc function and creates a new process running the same program at the same point (or call site). This method returns a new Venus::Process object representing the child process (from within the execution of the child process (or fork)), and returns undef
to the parent (or originating) process. In list context, this method returns both the process and PID (or process ID) of the child process. If a callback or argument is provided it will be executed in the child process.
Since 0.06
- fork example 1
-
# given: synopsis; $process = $parent->fork; # if ($process) { # # in forked process ... # $process->exit; # } # else { # # in parent process ... # $parent->wait(-1); # } # in child process # bless({...}, 'Venus::Process')
- fork example 2
-
# given: synopsis; my $pid; ($process, $pid) = $parent->fork; # if ($process) { # # in forked process ... # $process->exit; # } # else { # # in parent process ... # $parent->wait($pid); # } # in parent process # (undef, $pid)
- fork example 3
-
# given: synopsis; my $pid; ($process, $pid) = $parent->fork(sub{ $$_{started} = time; }); # if ($process) { # # in forked process ... # $process->exit; # } # else { # # in parent process ... # $parent->wait($pid); # } # in parent process # (undef, $pid)
- fork example 4
-
# given: synopsis; $process = $parent->fork(sub{}); # simulate fork failure # no forking attempted if NOT supported # Exception! (isa Venus::Process:Error) (see error_on_fork_support)
- fork example 5
-
# given: synopsis $process = $parent->do('alarm', 10)->fork; # if ($process) { # # in forked process with alarm installed ... # $process->exit; # } # else { # # in parent process ... # $parent->wait(-1); # } # in child process # bless({...}, 'Venus::Process')
forks
forks(Str | CodeRef $code, Any @args) (Process, ArrayRef[Int])
The forks method creates multiple forks by calling the "fork" method n
times, based on the count specified. As with the "fork" method, this method returns a new Venus::Process object representing the child process (from within the execution of the child process (or fork)), and returns undef
to the parent (or originating) process. In list context, this method returns both the process and an arrayref of PID values (or process IDs) for each of the child processes created. If a callback or argument is provided it will be executed in each child process.
Since 0.06
- forks example 1
-
# given: synopsis; $process = $parent->forks(5); # if ($process) { # # do something in (each) forked process ... # $process->exit; # } # else { # # do something in parent process ... # $parent->wait(-1); # } # bless({...}, 'Venus::Process')
- forks example 2
-
# given: synopsis; my $pids; ($process, $pids) = $parent->forks(5); # if ($process) { # # do something in (each) forked process ... # $process->exit; # } # else { # # do something in parent process ... # $parent->wait($_) for @$pids; # } # in parent process # (undef, $pids)
- forks example 3
-
# given: synopsis; my $pids; ($process, $pids) = $parent->forks(5, sub{ my ($fork, $pid, $iteration) = @_; # $iteration is the fork iteration index $fork->exit; }); # if ($process) { # # do something in (each) forked process ... # $process->exit; # } # else { # # do something in parent process ... # $parent->wait($_) for @$pids; # } # in child process # bless({...}, 'Venus::Process')
is_follower
is_follower() (Bool)
The is_follower method returns true if the process is not the "leader", otherwise returns false.
Since 2.91
- is_follower example 1
-
package main; use Venus::Process; my $process = Venus::Process->new; my $is_follower = $process->is_follower; # false
- is_follower example 2
-
package main; use Venus::Process; my $process_1 = Venus::Process->new(12345)->register; my $process_2 = Venus::Process->new(12346)->register; my $process_3 = Venus::Process->new(12347)->register; my $is_follower = $process_1->is_follower; # false # my $is_follower = $process_2->is_follower; # true # my $is_follower = $process_3->is_follower; # true
- is_follower example 3
-
package main; use Venus::Process; my $process_1 = Venus::Process->new(12345)->register; my $process_2 = Venus::Process->new(12346)->register; my $process_3 = Venus::Process->new(12347)->register; # my $is_follower = $process_1->is_follower; # false my $is_follower = $process_2->is_follower; # true # my $is_follower = $process_3->is_follower; # true
is_leader
is_leader() (Bool)
The is_leader method returns true if the process is the "leader", otherwise returns false.
Since 2.91
- is_leader example 1
-
package main; use Venus::Process; my $process = Venus::Process->new; my $is_leader = $process->is_leader; # true
- is_leader example 2
-
package main; use Venus::Process; my $process_1 = Venus::Process->new(12345)->register; my $process_2 = Venus::Process->new(12346)->register; my $process_3 = Venus::Process->new(12347)->register; my $is_leader = $process_1->is_leader; # true # my $is_leader = $process_2->is_leader; # false # my $is_leader = $process_3->is_leader; # false
- is_leader example 3
-
package main; use Venus::Process; my $process_1 = Venus::Process->new(12345)->register; my $process_2 = Venus::Process->new(12346)->register; my $process_3 = Venus::Process->new(12347)->register; # my $is_leader = $process_1->is_leader; # true my $is_leader = $process_2->is_leader; # false # my $is_leader = $process_3->is_leader; # false
is_registered
is_registered() (Bool)
The is_registered method returns true if the process has registered using the "register" method, otherwise returns false.
Since 2.91
- is_registered example 1
-
package main; use Venus::Process; my $process = Venus::Process->new; my $is_registered = $process->is_registered; # false
- is_registered example 2
-
package main; use Venus::Process; my $process = Venus::Process->new(12345)->register; my $is_registered = $process->is_registered; # true
is_unregistered
is_unregistered() (Bool)
The is_unregistered method returns true if the process has unregistered using the "unregister" method, or had never registered at all, otherwise returns false.
Since 2.91
- is_unregistered example 1
-
package main; use Venus::Process; my $process = Venus::Process->new; my $is_unregistered = $process->is_unregistered; # true
- is_unregistered example 2
-
package main; use Venus::Process; my $process = Venus::Process->new(12345); my $is_unregistered = $process->is_unregistered; # true
- is_unregistered example 3
-
package main; use Venus::Process; my $process = Venus::Process->new(12345)->register; my $is_unregistered = $process->is_unregistered; # false
join
join(Str $name) (Process)
The join method sets the "exchange", registers the process with the exchange using "register", and clears the "watchlist", then returns the invocant.
Since 2.91
- join example 1
-
package main; use Venus::Process; my $process = Venus::Process->new; $process = $process->join; # bless({...}, 'Venus::Process')
- join example 2
-
package main; use Venus::Process; my $process = Venus::Process->new; $process = $process->join('procs'); # bless({...}, 'Venus::Process')
kill
kill(Str $signal, Int @pids) (Int)
The kill method calls the system "kill" in perlfunc function which sends a signal to a list of processes and returns truthy or falsy. Note: A truthy result doesn't necessarily mean all processes were successfully signalled.
Since 0.06
- kill example 1
-
# given: synopsis; if ($process = $parent->fork) { # in forked process ... $process->exit; } my $kill = $parent->kill('term', int $process); # 1
killall
killall(Str $name, Int @pids) (ArrayRef)
The killall method accepts a list of PIDs (or uses the "watchlist" if not provided) and returns the result of calling the "kill" method for each PID. Returns a list in list context.
Since 2.40
- killall example 1
-
# given: synopsis package main; if ($process = $parent->fork) { # in forked process ... $process->exit; } my $killall = $parent->killall('term'); # [1]
- killall example 2
-
# given: synopsis package main; if ($process = $parent->fork) { # in forked process ... $process->exit; } my $killall = $parent->killall('term', 1001..1004); # [1, 1, 1, 1]
leader
leader() (Int)
The leader method uses a simple leader election algorithm to determine the process leader and returns the PID for that process. The leader is always the lowest value active PID (i.e. that responds to "ping").
Since 2.91
- leader example 2
-
# given: synopsis package main; # in process 1 my $process_1 = Venus::Process->new(12345)->register; # in process 2 my $process_2 = Venus::Process->new(12346)->register; # in process 3 my $process_3 = Venus::Process->new(12347)->register; # in process 1 my $leader = $process_3->leader; # 12345
leave
leave(Str $name) (Process)
The leave method sets the "exchange" to undefined, unregisters the process using "unregister", and clears the "watchlist", then returns the invocant.
Since 2.91
- leave example 1
-
package main; use Venus::Process; my $process = Venus::Process->new; $process = $process->leave; # bless({...}, 'Venus::Process')
- leave example 2
-
package main; use Venus::Process; my $process = Venus::Process->new; $process = $process->leave('procs'); # bless({...}, 'Venus::Process')
limit
limit(Int $count) (Bool)
The limit method blocks the execution of the current process until the number of processes in the "watchlist" falls bellow the count specified. The method returns true once execution continues if execution was blocked, and false if the limit has yet to be reached.
Since 3.40
- limit example 1
-
package main; use Venus::Process; my $parent = Venus::Process->new; $parent->work(sub { my ($process) = @_; # in forked process ... $process->exit; }); my $limit = $parent->limit(2); # false
- limit example 2
-
package main; use Venus::Process; my $parent = Venus::Process->new; $parent->works(2, sub { my ($process) = @_; # in forked process ... $process->exit; }); my $limit = $parent->limit(2); # true
others
others() (ArrayRef)
The others method returns all "registrants" other than the current process, i.e. all other registered process PIDs whether active or inactive.
Since 2.91
- others example 2
-
# given: synopsis package main; # in process 1 my $process_1 = Venus::Process->new(12345)->register; # in process 2 my $process_2 = Venus::Process->new(12346)->register; # in process 3 my $process_3 = Venus::Process->new(12347)->register; # in process 1 my $others = $process_1->others; # [12346, 12347]
others_active
others_active() (ArrayRef)
The others_active method returns all "registrants" other than the current process which are active, i.e. all other registered process PIDs that responds to "ping".
Since 2.91
- others_active example 1
-
# given: synopsis package main; # in process 1 my $process_1 = Venus::Process->new(12345)->register; # in process 2 my $process_2 = Venus::Process->new(12346)->register; # in process 3 my $process_3 = Venus::Process->new(12347)->register; # in process 1 my $others_active = $process_1->others_active; # [12346, 12347]
others_inactive
others_inactive() (ArrayRef)
The others_inactive method returns all "registrants" other than the current process which are inactive, i.e. all other registered process PIDs that do not respond to "ping".
Since 2.91
- others_inactive example 1
-
# given: synopsis package main; # in process 1 my $process_1 = Venus::Process->new(12345)->register; # in process 2 my $process_2 = Venus::Process->new(12346)->register; # in process 3 my $process_3 = Venus::Process->new(12347)->register; # in process 1 (assuming all processes exited) my $others_inactive = $process_1->others_inactive; # [12346, 12347]
pid
pid() (Int)
The pid method returns the PID of the current process.
Since 2.40
- pid example 1
-
package main; use Venus::Process; my $parent = Venus::Process->new; my $pid = $parent->pid; # 00000
pids
pids() (ArrayRef)
The pids method returns the PID of the current process, and the PIDs of any child processes.
Since 2.40
- pids example 1
-
package main; use Venus::Process; my $parent = Venus::Process->new; my $pids = $parent->pids; # [00000]
- pids example 2
-
package main; use Venus::Process; my $parent = Venus::Process->new; $parent->watch(1001..1004); my $pids = $parent->pids; # [00000, 1001..1004]
ping
ping(Int @pids) (Int)
The ping method returns truthy if the process of the PID provided is active. If multiple PIDs are provided, this method will return the count of active PIDs.
Since 2.01
- ping example 1
-
# given: synopsis; if ($process = $parent->fork) { # in forked process ... $process->exit; } my $ping = $parent->ping(int $process); # 1
poll
poll(Int $timeout, Str | CodeRef $code, Any @args) (ArrayRef)
The poll method continuously calls the named method or coderef and returns the result that's not undefined, or throws an exception on timeout. If no method name is provided this method will default to calling "recvall".
Since 2.91
- poll example 1
-
# given: synopsis package main; my $poll = $parent->poll(0, 'ping', $parent->pid); # [1]
- poll example 2
-
# given: synopsis package main; my $poll = $parent->poll(5, 'ping', $parent->pid); # [1]
- poll example 3
-
# given: synopsis package main; my $poll = $parent->poll(0, 'recv', $parent->pid); # Exception! (isa Venus::Process::Error) (see error_on_timeout_poll)
- poll example 4
-
# given: synopsis package main; my $poll = $parent->poll(5, sub { int(rand(2)) ? "" : () }); # [""]
pool
pool(Int $count, Int $timeout) (Process)
The pool method blocks the execution of the current process until the number of "other" processes are registered and pingable. This method returns the invocant when successful, or throws an exception if the operation timed out.
Since 2.91
- pool example 1
-
# given: synopsis package main; # in process 1 my $process_1 = Venus::Process->new(12345)->register; # in process 2 my $process_2 = Venus::Process->new(12346)->register; # in process 1 $process_1 = $process_1->pool; # bless({...}, 'Venus::Process')
- pool example 2
-
# given: synopsis package main; # in process 1 my $process_1 = Venus::Process->new(12345)->register; # in process 2 my $process_2 = Venus::Process->new(12346)->register; # in process 3 my $process_3 = Venus::Process->new(12347)->register; # in process 1 $process_1 = $process_1->pool(2); # bless({...}, 'Venus::Process')
- pool example 3
-
# given: synopsis package main; # in process 1 my $process_1 = Venus::Process->new(12345)->register; # in process 2 my $process_2 = Venus::Process->new(12346)->register; # in process 3 my $process_3 = Venus::Process->new(12347)->register; # in process 1 $process_1 = $process_1->pool(3, 0); # Exception! (isa Venus::Process::Error) (see error_on_timeout_pool)
ppid
ppid() (Int)
The ppid method returns the PID of the parent process (i.e. the process which forked the current process, if any).
Since 2.91
- ppid example 2
-
# given: synopsis; $process = $parent->fork; # in child process my $ppid = $process->ppid; # 00000
prune
prune() (Process)
The prune method removes all stopped processes and returns the invocant.
Since 2.40
- prune example 1
-
package main; use Venus::Process; my $parent = Venus::Process->new; $parent->watch(1001); $parent = $parent->prune; # bless({...}, 'Venus::Process')
- prune example 2
-
package main; use Venus::Process; my $parent = Venus::Process->new; my $process = $parent->fork; if ($process) { # in forked process ... $process->exit; } $parent = $parent->prune; # bless({...}, 'Venus::Process')
- prune example 3
-
package main; use Venus::Process; my $parent = Venus::Process->new; $parent->work(sub { my ($process) = @_; # in forked process ... $process->exit; }); $parent = $parent->prune; # bless({...}, 'Venus::Process')
recall
recall(Int $pid) (Any)
The recall method returns the earliest message, sent by the current process to the process specified by the PID provided, which is no longer active (i.e. responding to "ping").
Since 2.91
- recall example 1
-
# given: synopsis package main; # in process 1 my $process_1 = Venus::Process->new(12345)->register; # in process 2 my $process_2 = Venus::Process->new(12346)->register; # in process 1 $process_1->send($process_2->pid, {from => $process_1->pid}); # in process 1 (process 2) my $recall = $process_1->recall($process_2->pid); # {from => 12345}
recallall
recallall() (ArrayRef)
The recallall method performs a "recall" on the parent process (if any) via "ppid" and any process listed in the "watchlist", and returns the results.
Since 2.91
- recallall example 1
-
# given: synopsis package main; # in process 1 my $process_1 = Venus::Process->new(12345)->register; # in process 2 my $process_2 = Venus::Process->new(12346)->register; # in process 3 my $process_3 = Venus::Process->new(12347)->register; # in process 1 $process_1->send($process_2->pid, {from => $process_1->pid}); $process_1->send($process_3->pid, {from => $process_1->pid}); $process_1->watch($process_2->pid, $process_3->pid); # in process 1 (process 2 and 3 died) my $recallall = $process_1->recallall; # [{from => 12345}, {from => 12345}]
recv
recv(Int $pid) (Any)
The recv method returns the earliest message found from the process specified by the PID provided.
Since 2.91
- recv example 2
-
# given: synopsis package main; # in process 1 my $process_1 = Venus::Process->new(12345); # in process 2 my $process_2 = Venus::Process->new(12346); # in process 1 my $recv = $process_1->recv($process_2->pid); # undef # in process 2 $process_2->send($process_1->pid, {from => $process_2->pid, said => 'hello'}); # in process 1 $recv = $process_1->recv($process_2->pid); # {from => 12346, said => 'hello'}
recvall
recvall() (ArrayRef)
The recvall method performs a "recv" on the parent process (if any) via "ppid" and any process listed in the "watchlist", and returns the results.
Since 2.91
- recvall example 1
-
# given: synopsis package main; # in process 1 my $process_1 = Venus::Process->new(12345)->register; # in process 2 my $process_2 = Venus::Process->new(12346)->register; $process_2->send($process_1->pid, {from => $process_2->pid}); # in process 3 my $process_3 = Venus::Process->new(12347)->register; $process_3->send($process_1->pid, {from => $process_3->pid}); # in process 1 my $recvall = $process_1->pool(2)->recvall; # [{from => 12346}, {from => 12347}]
- recvall example 2
-
# given: synopsis package main; # in process 1 my $process_1 = Venus::Process->new(12345)->register; # in process 2 my $process_2 = Venus::Process->new(12346)->register; $process_2->send($process_1->pid, {from => $process_2->pid}); # in process 3 my $process_3 = Venus::Process->new(12347)->register; $process_3->send($process_1->pid, {from => $process_3->pid}); # in process 1 my $recvall = $process_1->pool(2)->recvall; # [{from => 12346}, {from => 12347}] # in process 2 $process_2->send($process_1->pid, {from => $process_2->pid}); # in process 1 $recvall = $process_1->recvall; # [{from => 12346}]
register
register() (Process)
The register method declares that the process is willing to cooperate with others (e.g. "send" nad "recv" messages), in a way that's discoverable by other processes, and returns the invocant.
Since 2.91
- register example 1
-
# given: synopsis package main; my $register = $parent->register; # bless({...}, 'Venus::Process')
registrants
registrants() (ArrayRef)
The registrants method returns the PIDs for all the processes that registered using the "register" method whether they're currently active or not.
Since 2.91
- registrants example 1
-
# given: synopsis package main; # in process 1 my $process_1 = Venus::Process->new(12345)->register; # in process 2 my $process_2 = Venus::Process->new(12346)->register; # in process 3 my $process_3 = Venus::Process->new(12347)->register; # in process 1 my $registrants = $process_1->registrants; # [12345, 12346, 12347]
restart
restart(CodeRef $callback) (ArrayRef)
The restart method executes the callback provided for each PID returned by the "stopped" method, passing the pid and the results of "check" to the callback as arguments, and returns the result of each call as an arrayref. In list context, this method returns a list.
Since 2.40
- restart example 1
-
# given: synopsis package main; $parent->watch(1001); my $restart = $parent->restart(sub { my ($pid, $check, $exit) = @_; # redeploy stopped process return [$pid, $check, $exit]; }); # [[1001, 1001, 255]]
send
send(Int $pid, Any $data) (Process)
The send method makes the data provided available to the process specified by the PID provided.
Since 2.91
- send example 1
-
# given: synopsis package main; my $send = $parent->send; # bless({...}, 'Venus::Process')
- send example 2
-
# given: synopsis package main; # in process 1 my $process_1 = Venus::Process->new(12345); # in process 2 my $process_2 = Venus::Process->new(12346); # in process 1 $process_1 = $process_1->send($process_2->pid, { from => $process_1->pid, said => 'hello', }); # bless({...}, 'Venus::Process') # in process 2 # $process_2->recv($process_1->pid); # {from => 12345, said => 'hello'}
sendall
sendall(Any $data) (Process)
The sendall method performs a "send" on the parent process (if any) via "ppid" and any process listed in the "watchlist", and returns the invocant.
Since 2.91
- sendall example 1
-
# given: synopsis package main; # in process 1 my $process_1 = Venus::Process->new(12345)->register; # in process 2 my $process_2 = Venus::Process->new(12346)->register; # in process 3 my $process_3 = Venus::Process->new(12347)->register; # in process 1 $process_1 = $process_1->pool(2)->sendall({ from => $process_1->pid, said => 'hello', }); # bless({...}, 'Venus::Process')
serve
serve(Int $count, CodeRef $callback) (Process)
The serve method executes the callback using "work" until "limit" blocks the execution of the current process, indefinitely. It has the effect of serving the callback and maintaining the desired number of forks until killed or gracefully shutdown.
Since 3.40
- serve example 1
-
package main; use Venus::Process; my $parent = Venus::Process->new; $parent->serve(2, sub { my ($process) = @_; # in forked process ... $process->exit; }); # ... # bless({...}, "Venus::Process")
- serve example 2
-
package main; use Venus::Process; my $parent = Venus::Process->new; $parent->serve(10, sub { my ($process) = @_; # in forked process ... $process->exit; }); # ... # bless({...}, "Venus::Process")
setsid
setsid() (Int)
The setsid method calls the "setsid" in POSIX function and sets the process group identifier of the current process.
Since 0.06
- setsid example 2
-
# given: synopsis; my $setsid = $parent->setsid; # Exception! (isa Venus::Process::Error) (see error_on_setid)
started
started() (ArrayRef)
The started method returns a list of PIDs whose processes have been started and which have not terminated. Returns a list in list context.
Since 2.40
- started example 1
-
# given: synopsis package main; my $started = $parent->started; # child not terminated # [...]
- started example 2
-
# given: synopsis package main; my $started = $parent->started; # child terminated # []
status
status(CodeRef $callback) (ArrayRef)
The status method executes the callback provided for each PID in the "watchlist", passing the pid and the results of "check" to the callback as arguments, and returns the result of each call as an arrayref. In list context, this method returns a list.
Since 2.40
- status example 1
-
package main; use Venus::Process; my $parent = Venus::Process->new; $parent->watch(12346); my $status = $parent->status(sub { my ($pid, $check, $exit) = @_; # assuming PID 12346 is still running (not terminated) return [$pid, $check, $exit]; }); # [[12346, 0, -1]]
- status example 2
-
package main; use Venus::Process; my $parent = Venus::Process->new; $parent->watch(12346); my $status = $parent->status(sub { my ($pid, $check, $exit) = @_; # assuming process 12346 terminated with exit code 255 return [$pid, $check, $exit]; }); # [[12346, 12346, 255]]
- status example 3
-
package main; use Venus::Process; my $parent = Venus::Process->new; $parent->watch(12346); my @status = $parent->status(sub { my ($pid, $check, $exit) = @_; # assuming process 12346 terminated with exit code 255 return [$pid, $check, $exit]; }); # ([12346, 12346, 255])
stderr
stderr(Str $path) (Process)
The stderr method redirects STDERR
to the path provided, typically /dev/null
or some equivalent. If called with no arguments STDERR
will be restored to its default.
Since 0.06
- stderr example 2
-
# given: synopsis; $parent = $parent->stderr('/nowhere'); # Exception! (isa Venus::Process:Error) (see error_on_stderr)
stdin
stdin(Str $path) (Process)
The stdin method redirects STDIN
to the path provided, typically /dev/null
or some equivalent. If called with no arguments STDIN
will be restored to its default.
Since 0.06
- stdin example 2
-
# given: synopsis; $parent = $parent->stdin('/nowhere'); # Exception! (isa Venus::Process::Error) (see error_on_stdin)
stdout
stdout(Str $path) (Process)
The stdout method redirects STDOUT
to the path provided, typically /dev/null
or some equivalent. If called with no arguments STDOUT
will be restored to its default.
Since 0.06
- stdout example 2
-
# given: synopsis; $parent = $parent->stdout('/nowhere'); # Exception! Venus::Process::Error (error_on_stdout)
stopped
stopped() (ArrayRef)
The stopped method returns a list of PIDs whose processes have terminated. Returns a list in list context.
Since 2.40
- stopped example 1
-
# given: synopsis package main; my $stopped = $parent->stopped; # child terminated # [...]
- stopped example 2
-
# given: synopsis package main; my $stopped = $parent->stopped; # child not terminated # []
sync
sync(Int $count, Int $timeout) (Process)
The sync method blocks the execution of the current process until the number of "other" processes are registered, pingable, and have each sent at-least one message to the current process. This method returns the invocant when successful, or throws an exception if the operation timed out.
Since 2.91
- sync example 1
-
# given: synopsis package main; # in process 1 my $process_1 = Venus::Process->new(12345)->register; # in process 2 my $process_2 = Venus::Process->new(12346)->register; $process_2->send($process_1->pid, {from => $process_2->pid, said => "hello"}); # in process 1 $process_1 = $process_1->sync; # bless({...}, 'Venus::Process')
- sync example 2
-
# given: synopsis package main; # in process 1 my $process_1 = Venus::Process->new(12345)->register; # in process 2 my $process_2 = Venus::Process->new(12346)->register; $process_2->send($process_1->pid, {from => $process_2->pid, said => "hello"}); # in process 3 my $process_3 = Venus::Process->new(12347)->register; $process_3->send($process_1->pid, {from => $process_3->pid, said => "hello"}); # in process 1 $process_1 = $process_1->sync(2); # bless({...}, 'Venus::Process')
- sync example 3
-
# given: synopsis package main; # in process 1 my $process_1 = Venus::Process->new(12345)->register; # in process 2 my $process_2 = Venus::Process->new(12346)->register; $process_2->send($process_1->pid, {from => $process_2->pid, said => "hello"}); # in process 3 my $process_3 = Venus::Process->new(12347)->register; $process_3->send($process_1->pid, {from => $process_3->pid, said => "hello"}); # in process 1 $process_1 = $process_1->sync(3, 0); # Exception! (isa Venus::Process::Error) (see error_on_timeout_sync)
trap
trap(Str $name, Str | CodeRef $expr) (Process)
The trap method registers a process signal trap (or callback) which will be invoked whenever the current process receives that matching signal. The signal traps are globally installed and will overwrite any preexisting behavior. Signal traps are inherited by child processes (or forks) but can be overwritten using this method, or reverted to the default behavior by using the "untrap" method.
Since 0.06
- trap example 1
-
# given: synopsis; $parent = $parent->trap(term => sub{ die 'Something failed!'; }); # bless({...}, 'Venus::Process')
unregister
unregister() (Process)
The unregister method declares that the process is no longer willing to cooperate with others (e.g. "send" nad "recv" messages), and will no longer be discoverable by other processes, and returns the invocant.
Since 2.91
- unregister example 1
-
# given: synopsis package main; my $unregister = $parent->unregister; # bless({...}, 'Venus::Process')
untrap
untrap(Str $name) (Process)
The untrap method restores the process signal trap specified to its default behavior. If called with no arguments, it restores all signal traps overwriting any user-defined signal traps in the current process.
Since 0.06
- untrap example 1
-
# given: synopsis; $parent->trap(chld => 'ignore')->trap(term => sub{ die 'Something failed!'; }); $parent = $parent->untrap('term'); # bless({...}, 'Venus::Process')
- untrap example 2
-
# given: synopsis; $parent->trap(chld => 'ignore')->trap(term => sub{ die 'Something failed!'; }); $parent = $parent->untrap; # bless({...}, 'Venus::Process')
unwatch
unwatch(Int @pids) (ArrayRef)
The unwatch method removes the PIDs provided from the watchlist and returns the list of PIDs remaining to be watched. In list context returns a list.
Since 2.40
- unwatch example 1
-
package main; use Venus::Process; my $parent = Venus::Process->new; my $unwatch = $parent->unwatch; # []
- unwatch example 2
-
package main; use Venus::Process; my $parent = Venus::Process->new; $parent->watch(1001..1004); my $unwatch = $parent->unwatch(1001); # [1002..1004]
- unwatch example 3
-
package main; use Venus::Process; my $parent = Venus::Process->new; $parent->watch(1001..1004); my $unwatch = $parent->unwatch(1002, 1004); # [1001, 1003]
wait
wait(Int $pid) (Int, Int)
The wait method does a blocking "waitpid" in perlfunc operation and returns the wait status. In list context, returns the specified process' exit code (if terminated).
Since 0.06
- wait example 1
-
package main; use Venus::Process; my $parent = Venus::Process->new; my ($process, $pid) = $parent->fork; if ($process) { # in forked process ... $process->exit; } my $wait = $parent->wait($pid); # 0
- wait example 2
-
package main; use Venus::Process; my $parent = Venus::Process->new; my ($process, $pid) = $parent->fork; if ($process) { # in forked process ... $process->exit; } my ($wait, $status) = $parent->wait('00000'); # (-1, -1)
- wait example 3
-
package main; use Venus::Process; my $parent = Venus::Process->new; my ($process, $pid) = $parent->fork(sub{ $_->exit(1) }); if ($process) { # in forked process ... $process->exit; } my ($wait, $status) = $parent->wait($pid); # ($pid, 1)
waitall
waitall(Int @pids) (ArrayRef)
The waitall method does a blocking "wait" call for all processes based on the PIDs provided (or the PIDs returned by "watchlist" if not provided) and returns an arrayref of results from calling "wait" on each PID. Returns a list in list context.
Since 2.40
- waitall example 1
-
package main; use Venus::Process; my $parent = Venus::Process->new; my $waitall = $parent->waitall; # []
- waitall example 2
-
package main; use Venus::Process; my $parent = Venus::Process->new; my $waitall = $parent->waitall(1001); # [[1001, 0]]
- waitall example 3
-
package main; use Venus::Process; my $parent = Venus::Process->new; my ($process, $pid) = $parent->fork; if ($process) { # in forked process ... $process->exit; } my $waitall = $parent->waitall; # [[$pid, 0]]
watch
watch(Int @pids) (ArrayRef)
The watch method records PIDs to be watched, e.g. using the "status" method and returns all PIDs being watched. Returns a list in list context.
Since 2.40
- watch example 1
-
package main; use Venus::Process; my $parent = Venus::Process->new; my $watch = $parent->watch; # []
- watch example 2
-
package main; use Venus::Process; my $parent = Venus::Process->new; my $watch = $parent->watch(1001..1004); # [1001..1004]
- watch example 3
-
package main; use Venus::Process; my $parent = Venus::Process->new; my $watch = $parent->watch(1001..1004, 1001..1004); # [1001..1004]
- watch example 4
-
package main; use Venus::Process; my $parent = Venus::Process->new; $parent->watch(1001..1004); my $watch = $parent->watch; # [1001..1004]
- watch example 5
-
package main; use Venus::Process; my $parent = Venus::Process->new; my @watch = $parent->watch(1001..1004); # (1001..1004)
watchlist
watchlist() (ArrayRef)
The watchlist method returns the recorded PIDs. Returns a list in list context.
Since 2.40
- watchlist example 1
-
package main; use Venus::Process; my $parent = Venus::Process->new; my $watchlist = $parent->watchlist; # []
- watchlist example 2
-
package main; use Venus::Process; my $parent = Venus::Process->new; $parent->watch(1001..1004); my $watchlist = $parent->watchlist; # [1001..1004]
work
work(Str | CodeRef $code, Any @args) (Int)
The work method forks the current process, runs the callback provided in the child process, and immediately exits after. This method returns the PID of the child process. It is recommended to install an "alarm" in perlfunc in the child process (i.e. callback) to avoid creating zombie processes in situations where the parent process might exit before the child process is done working.
Since 0.06
- work example 1
-
# given: synopsis; my $pid = $parent->work(sub{ my ($process) = @_; # in forked process ... $process->exit; }); # $pid
works
works(Int $count, CodeRef $callback, Any @args) (ArrayRef)
The works method creates multiple forks by calling the "work" method n
times, based on the count specified. The works method runs the callback provided in the child process, and immediately exits after with an exit code of 0
by default. This method returns the PIDs of the child processes. It is recommended to install an "alarm" in perlfunc in the child process (i.e. callback) to avoid creating zombie processes in situations where the parent process might exit before the child process is done working.
Since 2.40
- works example 1
-
# given: synopsis; my $pids = $parent->works(5, sub{ my ($process) = @_; # in forked process ... $process->exit; }); # $pids
ERRORS
This package may raise the following errors:
- error:
error_on_chdir
-
This package may raise an error_on_chdir exception.
example 1
# given: synopsis; my $input = { throw => 'error_on_chdir', error => $!, path => '/nowhere', pid => 123, }; my $error = $parent->catch('error', $input); # my $name = $error->name; # "on_chdir" # my $message = $error->render; # "Can't chdir \"$path\": $!" # my $path = $error->stash('path'); # "/nowhere" # my $pid = $error->stash('pid'); # 123
- error:
error_on_fork_process
-
This package may raise an error_on_fork_process exception.
example 1
# given: synopsis; my $input = { throw => 'error_on_fork_process', error => $!, pid => 123, }; my $error = $parent->catch('error', $input); # my $name = $error->name; # "on_fork_process" # my $message = $error->render; # "Can't fork process $pid: $!" # my $pid = $error->stash('pid'); # "123"
- error:
error_on_fork_support
-
This package may raise an error_on_fork_support exception.
example 1
# given: synopsis; my $input = { throw => 'error_on_fork_support', pid => 123, }; my $error = $parent->catch('error', $input); # my $name = $error->name; # "on_fork_support" # my $message = $error->render; # "Can't fork process $pid: Fork emulation not supported" # my $pid = $error->stash('pid'); # 123
- error:
error_on_setid
-
This package may raise an error_on_setid exception.
example 1
# given: synopsis; my $input = { throw => 'error_on_setid', error => $!, pid => 123, }; my $error = $parent->catch('error', $input); # my $name = $error->name; # "on_setid" # my $message = $error->render; # "Can't start a new session: $!" # my $pid = $error->stash('pid'); # 123
- error:
error_on_stderr
-
This package may raise an error_on_stderr exception.
example 1
# given: synopsis; my $input = { throw => 'error_on_stderr', error => $!, path => "/nowhere", pid => 123, }; my $error = $parent->catch('error', $input); # my $name = $error->name; # "on_stderr" # my $message = $error->render; # "Can't redirect STDERR to \"/nowhere\": $!" # my $path = $error->stash('path'); # "/nowhere" # my $pid = $error->stash('pid'); # 123
- error:
error_on_stdin
-
This package may raise an error_on_stdin exception.
example 1
# given: synopsis; my $input = { throw => 'error_on_stdin', error => $!, path => "/nowhere", pid => 123, }; my $error = $parent->catch('error', $input); # my $name = $error->name; # "on_stdin" # my $message = $error->render; # "Can't redirect STDIN to \"$path\": $!" # my $path = $error->stash('path'); # "/nowhere" # my $pid = $error->stash('pid'); # 123
- error:
error_on_stdout
-
This package may raise an error_on_stdout exception.
example 1
# given: synopsis; my $input = { throw => 'error_on_stdout', error => $!, path => "/nowhere", pid => 123, }; my $error = $parent->catch('error', $input); # my $name = $error->name; # "on_stdout" # my $message = $error->render; # "Can't redirect STDOUT to \"$path\": $!" # my $path = $error->stash('path'); # "/nowhere" # my $pid = $error->stash('pid'); # 123
- error:
error_on_timeout_poll
-
This package may raise an error_on_timeout_poll exception.
example 1
# given: synopsis; my $input = { throw => 'error_on_timeout_poll', code => sub{}, timeout => 0, }; my $error = $parent->catch('error', $input); # my $name = $error->name; # "on_timeout_poll" # my $message = $error->render; # "Timed out after 0 seconds in process 12345 while polling __ANON__" # my $code = $error->stash('code'); # sub{} # my $exchange = $error->stash('exchange'); # undef # my $pid = $error->stash('pid'); # 12345 # my $timeout = $error->stash('timeout'); # 0
- error:
error_on_timeout_pool
-
This package may raise an error_on_timeout_pool exception.
example 1
# given: synopsis; my $input = { throw => 'error_on_timeout_pool', pool_size => 2, timeout => 0, }; my $error = $parent->catch('error', $input); # my $name = $error->name; # "on_timeout_pool" # my $message = $error->render; # "Timed out after 0 seconds in process 12345 while pooling" # my $exchange = $error->stash('exchange'); # undef # my $pid = $error->stash('pid'); # 12345 # my $pool_size = $error->stash('pool_size'); # 2 # my $timeout = $error->stash('timeout'); # 0
- error:
error_on_timeout_sync
-
This package may raise an error_on_timeout_sync exception.
example 1
# given: synopsis; my $input = { throw => 'error_on_timeout_sync', pool_size => 2, timeout => 0, }; my $error = $parent->catch('error', $input); # my $name = $error->name; # "on_timeout_sync" # my $message = $error->render; # "Timed out after 0 seconds in process 12345 while syncing" # my $exchange = $error->stash('exchange'); # undef # my $pid = $error->stash('pid'); # 12345 # my $pool_size = $error->stash('pool_size'); # 2 # my $timeout = $error->stash('timeout'); # 0
OPERATORS
This package overloads the following operators:
- operation:
("")
-
This package overloads the
""
operator.example 1
# given: synopsis; my $result = "$parent"; # $pid
- operation:
(~~)
-
This package overloads the
~~
operator.example 1
# given: synopsis; my $result = $parent ~~ /^\d+$/; # 1
AUTHORS
Awncorp, awncorp@cpan.org
LICENSE
Copyright (C) 2000, Al Newkirk.
This program is free software, you can redistribute it and/or modify it under the terms of the Apache license version 2.0.