NAME
Forks::Super::Job - object representing a background task
VERSION
0.37
SYNOPSIS
use Forks::Super;
$pid = Forks::Super::fork( \%options ); # see Forks::Super
$job = Forks::Super::Job::get($pid);
$job = Forks::Super::Job::getByName($name);
print "Current job state is $job->{state}\n";
print "Job was created at ", scalar localtime($job->{created}), "\n";
with overloading
See "OVERLOADING".
use Forks::Super 'overload';
$job = Forks::Super::fork( \%options );
print "Process id of new job is $job\n";
print "Current state is ", $job->state, "\n";
waitpid $job, 0;
print "Exit status was ", $job->status, "\n";
DESCRIPTION
Calls to Forks::Super::fork()
that successfully spawn a child process or create a deferred job (see "Deferred processes" in Forks::Super) will cause a Forks::Super::Job
instance to be created to track the job's state. For many uses of fork()
, it will not be necessary to query the state of a background job. But access to these objects is provided for users who want to exercise even greater control over their use of background processes.
Calls to Forks::Super::fork()
that fail (return undef
or small negative numbers) generally do not cause a new Forks::Super::Job
instance to be created.
ATTRIBUTES
Use the Forks::Super::Job::get
or Forks::Super::Job::getByName
methods to obtain a Forks::Super::Job object for examination. The Forks::Super::Job::get
method takes a process ID or job ID as an input (a value that may have been returned from a previous call to Forks::Super::fork()
and returns a reference to a Forks::Super::Job
object, or undef
if the process ID or job ID was not associated with any known Job object. The Forks::Super::Job::getByName
looks up job objects by the name
parameter that may have been passed in the Forks::Super::fork()
call.
A Forks::Super::Job
object has many attributes, some of which may be of interest to an end-user. Most of these should not be overwritten.
- pid
-
Process ID or job ID. For deferred processes, this will be a unique large negative number (a job ID). For processes that were not deferred, this valud is the process ID of the child process that performed this job's task.
- real_pid
-
The process ID of the child process that performed this job's task. For deferred processes, this value is undefined until the job is launched and the child process is spawned.
- pgid
-
The process group ID of the child process. For deferred processes, this value is undefined until the child process is spawned. It is also undefined for systems that do not implement getpgrp.
- created
-
The time (since the epoch) at which the instance was created.
- start
-
The time at which a child process was created for the job. This value will be undefined until the child process is spawned.
- end
-
The time at which the child process completed and the parent process received a
SIGCHLD
signal for the end of this process. This value will be undefined until the child process is complete. - reaped
-
The time at which a job was reaped via a call to
Forks::Super::wait
,Forks::Super::waitpid
, orForks::Super::waitall
. Will be undefined until the job is reaped. - state
-
A string value indicating the current state of the job. Current allowable values are
DEFERRED
-
For jobs that are on the job queue and have not started yet.
ACTIVE
-
For jobs that have started in a child process
COMPLETE
-
For jobs that have completed and caused the parent process to receive a
SIGCHLD
signal, but have not been reaped. REAPED
-
For jobs that have been reaped by a call to
Forks::Super::wait
,Forks::Super::waitpid
, orForks::Super::waitall
. SUSPENDED
-
The job has started but it has been suspended (with a
SIGSTOP
or other appropriate mechanism for your operating system) and is not currently running. A suspended job will not consume CPU resources but my tie up memory, I/O, and network resources. SUSPENDED-DEFERRED
-
Job is in the job queue and has not started yet, and also the job has been suspended.
- status
-
The exit status of a job. See CHILD_ERROR in
perlvar
. Will be undefined until the job is complete. - style
-
One of the strings
natural
,cmd
, orsub
, indicating whether the initialfork
call returned from the child process or whether the child process was going to run a shell command or invoke a Perl subroutine and then exit. - cmd
-
The shell command to run that was supplied in the
fork
call. - sub
- args
-
The name of or reference to CODE to run and the subroutine arguments that were supplied in the
fork
call. - _on_busy
-
The behavior of this job in the event that the system was too "busy" to enable the job to launch. Will have one of the string values
block
,fail
, orqueue
. - queue_priority
-
If this job was deferred, the relative priority of this job.
- can_launch
-
By default undefined, but could be a CODE reference supplied in the
fork()
call. If defined, it is the code that runs when a job is ready to start to determine whether the system is too busy or not. - depend_on
-
If defined, contains a list of process IDs and job IDs that must complete before this job will be allowed to start.
- depend_start
-
If defined, contains a list of process IDs and job IDs that must start before this job will be allowed to start.
- start_after
-
Indicates the earliest time (since the epoch) at which this job may start.
- expiration
-
Indicates the latest time that this job may be allowed to run. Jobs that run past their expiration parameter will be killed.
- os_priority
-
Value supplied to the
fork
call about desired operating system priority for the job. - cpu_affinity
-
Value supplied to the
fork
call about desired CPU's for this process to prefer. - child_stdin
- child_stdout
- child_stderr
-
If the job has been configured for interprocess communication, these attributes correspond to the handles for passing standard input to the child process, and reading standard output and standard error from the child process, respectively.
FUNCTIONS
get
$job = Forks::Super::Job::get($pidOrName)
-
Looks up a
Forks::Super::Job
object by a process ID/job ID or name attribute and returns the job object. Returnsundef
for an unrecognized pid or job name. $n = Forks::Super::Job::count_active_processes()
-
Returns the current number of active background processes. This includes only
- 1. First generation processes. Not the children and grandchildren of child processes.
- 2. Processes spawned by the
Forks::Super
module, and not processes that may have been created outside theForks::Super
framework, say, by an explicit call toCORE::fork()
, a call likesystem("./myTask.sh &")
, or a form of Perl'sopen
function that launches an external command.
METHODS
A Forks::Super::Job
object recognizes the following methods. In general, these methods should only be used from the foreground process (the process that spawned the background job).
waitpid
$job->wait( [$timeout] )
$job->waitpid( $flags [,$timeout] )
-
Convenience method to wait until or test whether the specified job has completed. See Forks::Super::waitpid.
kill
$job->kill($signal)
-
Convenience method to send a signal to a background job. See Forks::Super::kill.
suspend
$job->suspend
-
When called on an active job, suspends the background process with
SIGSTOP
or other mechanism appropriate for the operating system.
resume
$job->resume
-
When called on a suspended job (see suspend, above), resumes the background process with
SIGCONT
or other mechanism appropriate for the operating system.
is_<state>
$job->is_complete
-
Indicates whether the job is in the
COMPLETE
orREAPED
state. $job->is_started
-
Indicates whether the job has started in a background process. While return a false value while the job is still in a deferred state.
$job->is_active
-
Indicates whether the specified job is currently running in a background process.
$job->is_suspended
-
Indicates whether the specified job has started but is currently in a suspended state.
toString
$job->toString()
$job->toShortString()
-
Outputs a string description of the important features of the job.
write_stdin
$job->write_stdin(@msg)
-
Writes the specified message to the child process's standard input stream, if the child process has been configured to receive input from interprocess communication. Writing to a closed handle or writing to a process that is not configured for IPC will result in a warning.
read_stdXXX
$line = $job->read_stdout()
@lines = $job->read_stdout()
$line = $job->read_stderr()
@lines = $job->read_stderr()
-
In scalar context, attempts to read a single line, and in list context, attempts to read all available lines from a child process's standard output or standard error stream.
If there is no available input, and if the
Forks::Super
module detects that the background job has completed (such that no more input will be created), then the file handle will automatically be closed. In scalar context, these methods will returnundef
if there is no input currently available on an inactive process, and""
(empty string) if there is no input available on an active process.Reading from a closed handle, or calling these methods on a process that has not been configured for IPC will result in a warning.
close_fh
$job->close_fh([@handle_id])
-
Closes IPC filehandles for the specified job. Optional input is one or more values from the set
stdin
,stdout
,stderr
, andall
to specify which filehandles to close. If no parameters are provided, the default behavior is to close all configured file handles.On most systems, open filehandles are a scarce resource and it is a very good practice to close filehandles when the jobs that created them are finished running and you are finished processing input and output on those filehandles.
OVERLOADING
An experimental feature in the Forks::Super module is to make it more convenient to access the functionality of Forks::Super::Job
. When this feature is enabled, the return value from a call to Forks::Super::fork()
is an overloaded Forks::Super::Job
object.
$job_or_pid = fork { %options };
In a numerical context, this value looks and behaves like a process ID (or job ID). The value can be passed to functions like kill
and waitpid
.
if ($job_or_pid != $another_pid) { ... }
kill 'TERM', $job_or_pid;
But you can also access the attributes and methods of the Forks::Super::Job
object.
$job_or_pid->{real_pid}
$job_or_pid->suspend
Even when overloading is enabled, Forks::Super::fork()
still returns a simple scalar value of 0 to the child process (when a value is returned).
Overloading is not enabled by default in this version of Forks::Super
. There are two ways you can enable this feature:
When this feature is enabled, the return value of Forks::Super::wait() and Forks::Super::waitpid() might also be an overload Forks::Super::Job
object. (But if wait
/waitpid
is returning an indicator value like 0
or -1
, then those return values are just simple scalars.)
- 1. Pass the
overload
parameter whenForks::Super
is loaded. -
use Forks::Super 'overload'; $job = fork { sub => { sleep 5 } }; print "New job: ", $job->toString(), "\n";
- 2. Call
Forks::Super::Job::enable_overload()
. -
Forks::Super::Job::enable_overload()
enables this feature at run-time.use Forks::Super; $pid = fork { cmd => [ "./mycommand.sh", "--42" ] }; print ref $pid; # empty string Forks::Super::Job::enable_overload(); $pid = fork { cmd => [ "./mycommand.sh", "--19" ] }; print ref $pid; # Forks::Super::Job
There is also a
Forks::Super::Job::disable_overload()
function to disable this feature at run-time. In principle, you should be able to enable and disable this feature as often as you wish.
SEE ALSO
AUTHOR
Marty O'Brien, <mob@cpan.org>
LICENSE AND COPYRIGHT
Copyright (c) 2009-2010, Marty O'Brien.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.
20 POD Errors
The following errors were encountered while parsing the POD:
- Around line 1172:
You forgot a '=back' before '=head3'
- Around line 1174:
'=item' outside of any '=over'
- Around line 1210:
You forgot a '=back' before '=head3'
- Around line 1212:
'=item' outside of any '=over'
- Around line 1219:
You forgot a '=back' before '=head3'
- Around line 1221:
'=item' outside of any '=over'
- Around line 1226:
You forgot a '=back' before '=head3'
- Around line 1228:
'=item' outside of any '=over'
- Around line 1233:
You forgot a '=back' before '=head3'
- Around line 1235:
'=item' outside of any '=over'
- Around line 1241:
You forgot a '=back' before '=head3'
- Around line 1243:
'=item' outside of any '=over'
- Around line 1262:
You forgot a '=back' before '=head3'
- Around line 1264:
'=item' outside of any '=over'
- Around line 1270:
You forgot a '=back' before '=head3'
- Around line 1272:
'=item' outside of any '=over'
- Around line 1280:
You forgot a '=back' before '=head3'
- Around line 1282:
'=item' outside of any '=over'
- Around line 1306:
You forgot a '=back' before '=head3'
- Around line 1308:
'=item' outside of any '=over'