http://www.perlmonks.org?node_id=807560

Hi,

i was trying http://gearman.org and i've therefor got from cpan Gearman::Client and Gearman::Client::Async but, for what i need, i felt like they weren't designed as i need, and i've tried to write another which suits best my needs.

Having not wrote any documentation, i'll just show you the usage:

use Gearman::OO::Client; my $client = Gearman::OO::Client->new( servers => ['127.0.0.1:4730'], max_queue => 4, ); logit("connected"); foreach my $id (1..8) { $client->submit( func=>'test', req=>"Data for $id", complete => sub { logit("completed #$id: @_"); }, error => sub { logit("ERROR #$id! @_"); }, ); logit("added #$id"); } logit("sync..."); $client->sync(); logit("done.");

and here the output with 2 workers which just sleep for 1 second and respond with an empty string1:

00:00:00 connected 00:00:00 added #1 00:00:00 added #2 00:00:00 added #3 00:00:00 added #4 00:00:01 completed #1: 00:00:01 completed #2: 00:00:01 added #5 00:00:01 added #6 00:00:02 completed #3: 00:00:02 completed #4: 00:00:02 added #7 00:00:02 added #8 00:00:02 sync... 00:00:03 completed #5: 00:00:03 completed #6: 00:00:04 completed #7: 00:00:04 completed #8: 00:00:04 done.

As you can see, some jobs are added early up to the max_queue limit, then it will block until any of the previous job complete to continue.

Also you can use a completely OO Job object:

package MyJob; use base 'Gearman::OO::Job'; sub new { my ($class, %args) = @_; bless \%args, $class; } sub function { 'test'; } sub complete { my ($self, $data) = @_; logit("$self completed($data)") +; } sub req_data { my ($self) = @_; return $self->{req}; } use Gearman::OO::Client; my $client = Gearman::OO::Client->new( servers => ['127.0.0.1:4730'], max_queue => 4, ); logit("connected"); foreach my $id (1..8) { $client->submit_job(MyJob->new(req => "OO #$id")); logit("added #$id"); } logit("sync..."); $client->sync(); logit("done.");

and now the meditation: anyone think this could be usefull? and if so, which name should i use? does it merit to be on CPAN?


1 -- on twp distinct shells: gearman -w -f test sleep 1