NAME
Thread::Isolate::Pool - A pool of threads to execute multiple tasks.
DESCRIPTION
This module creates a pool of threads that can be used to execute simultaneously many tasks. The interface to the pool is similar to a normal Thread::Isolate object, so we can think that the pool is like a thread that can receive multiple calls at the same time.
USAGE
use Thread::Isolate::Pool ;
my $pool = Thread::Isolate::Pool->new() ;
$pool->use('LWP::Simple') ; ## Loads LWP::Simple in the main thread of the pool.
print $pool->main_thread->err ; ## $@ of the main thread of the pool.
my $url = 'http://www.perlmonks.com/' ;
my $job1 = $pool->call_detached('get' , $url) ;
my $job2 = $pool->call_detached('get' , $url) ;
my $job3 = $pool->call_detached('get' , $url) ;
## Print what jobs are running in the pool:
while( $job1->is_running || $job2->is_running || $job3->is_running ) {
print "[1]" if $job1->is_running ;
print "[2]" if $job2->is_running ;
print "[3]" if $job3->is_running ;
}
print "\n<<1>> Size: " . length( $job1->returned ) . "\n" ;
print "\n<<2>> Size: " . length( $job2->returned ) . "\n" ;
print "\n<<3>> Size: " . length( $job3->returned ) . "\n" ;
## Shutdown all the thread of the pool:
$pool->shutdown ;
The code above creates a Pool of threads and make simultaneously 3 LWP::Simple::get()s. Internally the pool has a main thread that is used to create the execution threads.
The main thread should have all the resources/modules loaded before make any call()/eval() to the pool.
When a call()/eval() is made, if the pool doesn't have any thread free (without be executing any job), a new thread is created from the main thread, and is used to do the task. Note that no threads will be removed after be created since this won't free memory, so is better to let them there until shutdown().
METHODS
new ( LIMIT )
Creates a new pool. If LIMIT is defined will set the maximal number of threads inside the pool. So, this defines the maximal number of simultaneous calls that the pool can have.
main_thread
Returns the main thread.
limit
Returns the LIMIT of threads of the pool.
get_free_thread
Return a free thread. If is not possible to get a free thread and create a new due LIMIT, any thread in the pool will be returned.
If called in a ARRAY contest will return ( FREE_THREAD , ON_LIMIT ), where when ON_LIMIT is true indicates that was not possible to get a free thread or create a new free thread.
add_thread
Add a new thread if is not in the LIMIT.
use ( MODULE , ARGS )
Make an "use MODULE qw(ARGS)" call in the main thread of the pool.
call
Get a free thread and make a $thi-call()> on it.
call_detached
Get a free thread and make a $thi-call_detached()> on it.
eval
Get a free thread and make a $thi-eval()> on it.
eval_detached
Get a free thread and make a $thi-eval_detached()> on it.
shutdown
Shutdown all the threads of the pool.
SEE ALSO
Thread::Isolate, Thread::Isolate::Map.
AUTHOR
Graciliano M. P. <gmpassos@cpan.org>
I will appreciate any type of feedback (include your opinions and/or suggestions). ;-P
COPYRIGHT
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.