NAME

Thread::Workers - Creates a "boss" which feeds a queue, which is serviced by a pool of threads called "workers". This module aims to be lightweight with limited features. Its primary aim is to provide simple Boss/Worker thread management while keeping dependencies low.

This is currently in experimental and development state and will be solidified more over time, but it works as advertised now.

SYNOPSIS

use Thread::Workers;

my $pool = Thread::Workers->new();
$pool->set_boss_fetch_cb(\&function_returns_work);
$pool->set_boss_log_cb(\&function_processes_worker_returns);
$pool->set_worker_work_cb(\&function_does_work);
$pool->start_boss();
$pool->start_workers();

#internal control loops
# we have orders to increase the load! add 500 workers
for (1..500) { 
  $pool->add_worker();
}

#time to cleanup

$pool->stop_boss(); #signal boss thread to die
$pool->stop_workers(); #stop the workers, may leave unfinished items in queue.
# Or! 
$pool->stop_finish_work(); #gracefully stop boss and finish work queue, then shut down workers.

DESCRIPTION

Thread::Workers utilizes threads, Thread::Sempahore, and Thread::Queue to create a pool of workers which are serviced with work by a boss thread. The boss thread could be fed data from a socket listening on the master thread, or could have a routine to check a database for work.

The boss thread will do the following actions when it receives work:

For scalars or arrays, it will post it as a single item into the work queue. Your workers need to know how to deal with the data being presented.

For a hash, the boss expects the work to be presented with the keys being unique integers. The integers correspond to the order they are placed into the queue. { 0 => 'step 0', 1 => 'step 1', 2 => { cmd1 => 'some data } }

This will create 3 separate "work items" to be placed into the queue. If you need to feed your workers with a single block of data from a hash, you *must* assign it this way.

{ 0 => { cmd1 => 'data', cmd2 => 'data' } }

Currently there is no signal to tell the boss to refeed its queue back upstream, though the Thread::Pool object can be accessed via $pool->{_queue}. Future revisions may include a callback for this ability.

SEE ALSO

threads

Thread::Queue

Thread::Sempahore

If this doesn't suit your needs, see the following projects:

Thread::Pool - very similar in goals to this project with a larger feature set.

Gearman - client/server worker pool

TheSchwartz (or Helios) - DBI fed backend to pools of workers

Beanstalk - client/server worker pool

Hopkins - "a better cronjob" with work queues

AUTHOR

Kal Aeolian, <kalielaeolian@gmail.com<gt>

COPYRIGHT AND LICENSE

Copyright (C) 2012 by Kal Aeolian

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.14.2 or, at your option, any later version of Perl 5 you may have available.