Thread::Isolate::Pool

Thread::Isolate::Pool is a pool of threads to execute multiple tasks.
Download

Thread::Isolate::Pool Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Graciliano M. P.
  • Publisher web site:
  • http://search.cpan.org/~gmpassos/

Thread::Isolate::Pool Tags


Thread::Isolate::Pool Description

Thread::Isolate::Pool is a pool of threads to execute multiple tasks. Thread::Isolate::Pool is a pool of threads to execute multiple tasks.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 "" if $job1->is_running ; print "" if $job2->is_running ; print "" if $job3->is_running ; } print "n Size: " . length( $job1->returned ) . "n" ; print "n Size: " . length( $job2->returned ) . "n" ; print "n 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().METHODSnew ( 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_threadReturns the main thread.limitReturns the LIMIT of threads of the pool.get_free_threadReturn 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_threadAdd 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.callGet a free thread and make a $thi-call()> on it.call_detachedGet a free thread and make a $thi-call_detached()> on it.evalGet a free thread and make a $thi-eval()> on it.eval_detachedGet a free thread and make a $thi-eval_detached()> on it.shutdownShutdown all the threads of the pool. Requirements: · Perl


Thread::Isolate::Pool Related Software