Venus::Process - Process Class
Process Class for Perl 5
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;
This package provides methods for handling and forking processes.
This package has the following attributes:
alarm(number $seconds) (number)
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
2.40
# given: synopsis package main; my $alarm = $parent->alarm; # undef
# given: synopsis package main; my $alarm = $parent->alarm(10); # 10
This package inherits behaviors from:
Venus::Kind::Utility
This package integrates behaviors from:
Venus::Role::Accessible
Venus::Role::Buildable
Venus::Role::Explainable
Venus::Role::Valuable
This package provides the following methods:
async(coderef $code, any @args) (Venus::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
3.40
# given: synopsis; my $async = $parent->async(sub{ my ($process) = @_; # in forked process ... $process->exit; }); # bless({...}, 'Venus::Process')
await(number $timeout) (arrayref)
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.
0
# given: synopsis; my $async = $parent->async(sub{ ($process) = @_; # in forked process ... return 'done'; }); my $await = $async->await; # ['done']
# given: synopsis; my $async = $parent->async(sub{ ($process) = @_; # in forked process ... return {status => 'done'}; }); my $await = $async->await; # [{status => 'done'}]
# given: synopsis; my $async = $parent->async(sub{ ($process) = @_; # in forked process ... return 'done'; }); my ($await) = $async->await; # 'done'
# given: synopsis; my $async = $parent->async(sub{ ($process) = @_; # in forked process ... return {status => 'done'}; }); my ($await) = $async->await; # {status => 'done'}
# 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(string $path) (Venus::Process)
The chdir method changes the working directory the current process is operating within.
Since 0.06
0.06
# given: synopsis; $parent = $parent->chdir; # bless({...}, 'Venus::Process')
# given: synopsis; $parent = $parent->chdir('/tmp'); # bless({...}, 'Venus::Process')
# given: synopsis; $parent = $parent->chdir('/xyz'); # Exception! (isa Venus::Process::Error) (see error_on_chdir)
check(number $pid) (number, number)
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).
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
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)
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(string | coderef $code, any @args) (number)
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.
package main; use Venus::Process; my $parent = Venus::Process->new; my $count = $parent->count; # 0
package main; use Venus::Process; my $parent = Venus::Process->new; my ($pid) = $parent->watch(1001); my $count = $parent->count; # 1
package main; use Venus::Process; my $parent = Venus::Process->new; my ($pid) = $parent->watch(1001); my $count = $parent->count('watchlist'); # 1
daemon() (Venus::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.
# given: synopsis; my $daemon = $parent->daemon; # exits parent immediately # in forked process ... # $daemon->exit;
data(number @pids) (number)
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
2.91
# given: synopsis package main; my $data = $parent->data; # 0
# 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
# 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(string $data) (any)
The decode method accepts a string representation of a Perl value and returns the Perl value.
# given: synopsis package main; my $decode = $parent->decode("{ok=>1}"); # { ok => 1 }
disengage() (Venus::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.
/dev/null
# given: synopsis; $parent = $parent->disengage; # bless({...}, 'Venus::Process')
encode(any $data) (string)
The encode method accepts a Perl value and returns a string representation of that Perl value.
# given: synopsis package main; my $encode = $parent->encode({ok=>1}); # "{ok=>1}"
engage() (Venus::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.
# given: synopsis; $parent = $parent->engage; # bless({...}, 'Venus::Process')
exchange(string $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).
# given: synopsis package main; my $exchange = $parent->exchange; # undef
# given: synopsis package main; my $exchange = $parent->exchange('procs'); # "procs"
# given: synopsis package main; my $exchange = $parent->exchange('procs'); # "procs" $exchange = $parent->exchange; # "procs"
exit(number $status) (number)
The exit method exits the program immediately.
# given: synopsis; my $exit = $parent->exit; # 0
# given: synopsis; my $exit = $parent->exit(1); # 1
followers() (arrayref)
The followers method returns the list of PIDs registered under the current "exchange" who are not the "leader".
# given: synopsis package main; my $followers = $parent->followers; # []
# 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(string | coderef $code, any @args) (Venus::Process, number)
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.
undef
# 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')
# 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)
# 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)
# 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)
# 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(string | coderef $code, any @args) (Venus::Process, within[arrayref, number])
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.
n
# 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')
# 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)
# 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')
future(coderef $code, any @args) (Venus::Future)
The future method creates a new object via "async" which runs the callback asynchronously and returns a Venus::Future object with a promise which eventually resolves to the value emitted or error raised.
Since 3.55
3.55
# given: synopsis; my $future = $parent->future(sub{ my ($process) = @_; # in forked process ... $process->exit; }); # bless({...}, 'Venus::Future')
# given: synopsis; my $future = $parent->future(sub{ ($process) = @_; # in forked process ... return 'done'; }); # $future->fulfill; # true # $future->value; # 'done'
# given: synopsis; my $future = $parent->future(sub{ ($process) = @_; # in forked process ... return {status => 'done'}; }); # $future->fulfill; # true # $future->value # {status => 'done'}
# given: synopsis; my $future = $parent->future(sub{ ($process) = @_; # in forked process ... return ['done']; }); # $future->fulfill; # true # my ($await) = $future->value; # ['done']
# given: synopsis; my $future = $parent->future(sub{ ($process) = @_; # in forked process ... $process->sendall(['send 1', 'send 2', 'send 3']); $process->sendall(['send 4']); $process->sendall(['send 5']); return; }); # $future->fulfill; # true # my ($await) = $future->value; # ['send 1', 'send 2', 'send 3']
is_dyadic() (boolean)
The is_dyadic method returns true is the process is configured to exclusively communicate with one other process, otherwise returns false.
package main; use Venus::Process; my $process = Venus::Process->new; my $is_dyadic = $process->is_dyadic; # false
package main; use Venus::Process; my $process = Venus::Process->new->async(sub{ return 'done'; }); my $is_dyadic = $process->is_dyadic; # true
is_follower() (boolean)
The is_follower method returns true if the process is not the "leader", otherwise returns false.
package main; use Venus::Process; my $process = Venus::Process->new; my $is_follower = $process->is_follower; # false
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
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() (boolean)
The is_leader method returns true if the process is the "leader", otherwise returns false.
package main; use Venus::Process; my $process = Venus::Process->new; my $is_leader = $process->is_leader; # true
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
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() (boolean)
The is_registered method returns true if the process has registered using the "register" method, otherwise returns false.
package main; use Venus::Process; my $process = Venus::Process->new; my $is_registered = $process->is_registered; # false
package main; use Venus::Process; my $process = Venus::Process->new(12345)->register; my $is_registered = $process->is_registered; # true
is_unregistered() (boolean)
The is_unregistered method returns true if the process has unregistered using the "unregister" method, or had never registered at all, otherwise returns false.
package main; use Venus::Process; my $process = Venus::Process->new; my $is_unregistered = $process->is_unregistered; # true
package main; use Venus::Process; my $process = Venus::Process->new(12345); my $is_unregistered = $process->is_unregistered; # true
package main; use Venus::Process; my $process = Venus::Process->new(12345)->register; my $is_unregistered = $process->is_unregistered; # false
join(string $name) (Venus::Process)
The join method sets the "exchange", registers the process with the exchange using "register", and clears the "watchlist", then returns the invocant.
package main; use Venus::Process; my $process = Venus::Process->new; $process = $process->join; # bless({...}, 'Venus::Process')
package main; use Venus::Process; my $process = Venus::Process->new; $process = $process->join('procs'); # bless({...}, 'Venus::Process')
kill(string $signal, number @pids) (number)
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.
# given: synopsis; if ($process = $parent->fork) { # in forked process ... $process->exit; } my $kill = $parent->kill('term', int $process); # 1
killall(string $name, number @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.
# given: synopsis package main; if ($process = $parent->fork) { # in forked process ... $process->exit; } my $killall = $parent->killall('term'); # [1]
# 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() (number)
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").
# given: synopsis package main; my $leader = $parent->leader; # 12345
# 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
# given: synopsis package main; my $leader = $parent->register->leader; # 12345
leave(string $name) (Venus::Process)
The leave method sets the "exchange" to undefined, unregisters the process using "unregister", and clears the "watchlist", then returns the invocant.
package main; use Venus::Process; my $process = Venus::Process->new; $process = $process->leave; # bless({...}, 'Venus::Process')
package main; use Venus::Process; my $process = Venus::Process->new; $process = $process->leave('procs'); # bless({...}, 'Venus::Process')
limit(number $count) (boolean)
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.
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
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() (arrayref)
The others method returns all "registrants" other than the current process, i.e. all other registered process PIDs whether active or inactive.
# given: synopsis package main; my $others = $parent->others; # []
# 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() (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".
# 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() (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".
# 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() (number)
The pid method returns the PID of the current process.
package main; use Venus::Process; my $parent = Venus::Process->new; my $pid = $parent->pid; # 00000
pids() (arrayref)
The pids method returns the PID of the current process, and the PIDs of any child processes.
package main; use Venus::Process; my $parent = Venus::Process->new; my $pids = $parent->pids; # [00000]
package main; use Venus::Process; my $parent = Venus::Process->new; $parent->watch(1001..1004); my $pids = $parent->pids; # [00000, 1001..1004]
ping(number @pids) (number)
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
2.01
# given: synopsis; if ($process = $parent->fork) { # in forked process ... $process->exit; } my $ping = $parent->ping(int $process); # 1
poll(number $timeout, string | 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".
# given: synopsis package main; my $poll = $parent->poll(0, 'ping', $parent->pid); # [1]
# given: synopsis package main; my $poll = $parent->poll(5, 'ping', $parent->pid); # [1]
# given: synopsis package main; my $poll = $parent->poll(0, 'recv', $parent->pid); # Exception! (isa Venus::Process::Error) (see error_on_timeout_poll)
# given: synopsis package main; my $poll = $parent->poll(5, sub { int(rand(2)) ? "" : () }); # [""]
pool(number $count, number $timeout) (Venus::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.
# 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')
# 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')
# 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() (number)
The ppid method returns the PID of the parent process (i.e. the process which forked the current process, if any).
# given: synopsis; my $ppid = $parent->ppid; # undef
# given: synopsis; $process = $parent->fork; # in child process my $ppid = $process->ppid; # 00000
prune() (Venus::Process)
The prune method removes all stopped processes and returns the invocant.
package main; use Venus::Process; my $parent = Venus::Process->new; $parent->watch(1001); $parent = $parent->prune; # bless({...}, 'Venus::Process')
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')
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(number $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").
# 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() (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.
# 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(number $pid) (any)
The recv method returns the earliest message found from the process specified by the PID provided.
# given: synopsis package main; my $recv = $parent->recv; # undef
# 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() (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.
# 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}]
# 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() (Venus::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.
# given: synopsis package main; my $register = $parent->register; # bless({...}, 'Venus::Process')
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.
# 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(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.
# 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(number $pid, any $data) (Venus::Process)
The send method makes the data provided available to the process specified by the PID provided.
# given: synopsis package main; my $send = $parent->send; # bless({...}, 'Venus::Process')
# 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(any $data) (Venus::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.
# 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(number $count, coderef $callback) (Venus::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.
package main; use Venus::Process; my $parent = Venus::Process->new; $parent->serve(2, sub { my ($process) = @_; # in forked process ... $process->exit; }); # ... # bless({...}, "Venus::Process")
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() (number)
The setsid method calls the "setsid" in POSIX function and sets the process group identifier of the current process.
# given: synopsis; my $setsid = $parent->setsid; # 1
# given: synopsis; my $setsid = $parent->setsid; # Exception! (isa Venus::Process::Error) (see error_on_setid)
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.
# given: synopsis package main; my $started = $parent->started; # child not terminated # [...]
# given: synopsis package main; my $started = $parent->started; # child terminated # []
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.
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]]
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]]
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(string $path) (Venus::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.
STDERR
# given: synopsis; $parent = $parent->stderr; # bless({...}, 'Venus::Process')
# given: synopsis; $parent = $parent->stderr('/nowhere'); # Exception! (isa Venus::Process:Error) (see error_on_stderr)
stdin(string $path) (Venus::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.
STDIN
# given: synopsis; $parent = $parent->stdin; # bless({...}, 'Venus::Process')
# given: synopsis; $parent = $parent->stdin('/nowhere'); # Exception! (isa Venus::Process::Error) (see error_on_stdin)
stdout(string $path) (Venus::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.
STDOUT
# given: synopsis; $parent = $parent->stdout; # bless({...}, 'Venus::Process')
# given: synopsis; $parent = $parent->stdout('/nowhere'); # Exception! Venus::Process::Error (error_on_stdout)
stopped() (arrayref)
The stopped method returns a list of PIDs whose processes have terminated. Returns a list in list context.
# given: synopsis package main; my $stopped = $parent->stopped; # child terminated # [...]
# given: synopsis package main; my $stopped = $parent->stopped; # child not terminated # []
sync(number $count, number $timeout) (Venus::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.
# 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')
# 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')
# 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(string $name, string | coderef $expr) (Venus::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.
# given: synopsis; $parent = $parent->trap(term => sub{ die 'Something failed!'; }); # bless({...}, 'Venus::Process')
unregister() (Venus::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.
# given: synopsis package main; my $unregister = $parent->unregister; # bless({...}, 'Venus::Process')
untrap(string $name) (Venus::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.
# given: synopsis; $parent->trap(chld => 'ignore')->trap(term => sub{ die 'Something failed!'; }); $parent = $parent->untrap('term'); # bless({...}, 'Venus::Process')
# given: synopsis; $parent->trap(chld => 'ignore')->trap(term => sub{ die 'Something failed!'; }); $parent = $parent->untrap; # bless({...}, 'Venus::Process')
unwatch(number @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.
package main; use Venus::Process; my $parent = Venus::Process->new; my $unwatch = $parent->unwatch; # []
package main; use Venus::Process; my $parent = Venus::Process->new; $parent->watch(1001..1004); my $unwatch = $parent->unwatch(1001); # [1002..1004]
package main; use Venus::Process; my $parent = Venus::Process->new; $parent->watch(1001..1004); my $unwatch = $parent->unwatch(1002, 1004); # [1001, 1003]
wait(number $pid) (number, number)
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).
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
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)
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(number @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.
package main; use Venus::Process; my $parent = Venus::Process->new; my $waitall = $parent->waitall; # []
package main; use Venus::Process; my $parent = Venus::Process->new; my $waitall = $parent->waitall(1001); # [[1001, 0]]
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(number @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.
package main; use Venus::Process; my $parent = Venus::Process->new; my $watch = $parent->watch; # []
package main; use Venus::Process; my $parent = Venus::Process->new; my $watch = $parent->watch(1001..1004); # [1001..1004]
package main; use Venus::Process; my $parent = Venus::Process->new; my $watch = $parent->watch(1001..1004, 1001..1004); # [1001..1004]
package main; use Venus::Process; my $parent = Venus::Process->new; $parent->watch(1001..1004); my $watch = $parent->watch; # [1001..1004]
package main; use Venus::Process; my $parent = Venus::Process->new; my @watch = $parent->watch(1001..1004); # (1001..1004)
watchlist() (arrayref)
The watchlist method returns the recorded PIDs. Returns a list in list context.
package main; use Venus::Process; my $parent = Venus::Process->new; my $watchlist = $parent->watchlist; # []
package main; use Venus::Process; my $parent = Venus::Process->new; $parent->watch(1001..1004); my $watchlist = $parent->watchlist; # [1001..1004]
work(string | coderef $code, any @args) (number)
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.
# given: synopsis; my $pid = $parent->work(sub{ my ($process) = @_; # in forked process ... $process->exit; }); # $pid
works(number $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.
# given: synopsis; my $pids = $parent->works(5, sub{ my ($process) = @_; # in forked process ... $process->exit; }); # $pids
This package may raise the following errors:
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_on_fork_process
This package may raise an error_on_fork_process exception.
# 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_on_fork_support
This package may raise an error_on_fork_support exception.
# 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_on_ping
This package may raise an error_on_ping exception.
# given: synopsis; my $input = { throw => 'error_on_ping', pid => 123, }; my $error = $parent->catch('error', $input); # my $name = $error->name; # "on_ping" # my $message = $error->render; # "Process 123 not responding to ping" # my $pid = $error->stash('pid'); # "123"
error_on_setid
This package may raise an error_on_setid exception.
# 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_on_stderr
This package may raise an error_on_stderr exception.
# 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_on_stdin
This package may raise an error_on_stdin exception.
# 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_on_stdout
This package may raise an error_on_stdout exception.
# 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_on_timeout_poll
This package may raise an error_on_timeout_poll exception.
# 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_on_timeout_pool
This package may raise an error_on_timeout_pool exception.
# 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_on_timeout_sync
This package may raise an error_on_timeout_sync exception.
# 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
This package overloads the following operators:
("")
This package overloads the "" operator.
""
# given: synopsis; my $result = "$parent"; # $pid
(~~)
This package overloads the ~~ operator.
~~
# given: synopsis; my $result = $parent ~~ /^\d+$/; # 1
Awncorp, awncorp@cpan.org
awncorp@cpan.org
Copyright (C) 2000, Awncorp, awncorp@cpan.org.
This program is free software, you can redistribute it and/or modify it under the terms of the Apache license version 2.0.
To install Venus, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Venus
CPAN shell
perl -MCPAN -e shell install Venus
For more information on module installation, please visit the detailed CPAN module installation guide.