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

Replies are listed 'Best First'.
Re: Another Gearman perl module?
by ikegami (Patriarch) on Nov 16, 2009 at 21:12 UTC
    How about

    dist:
    Distributed-Gearman

    modules:
    Distributed::Gearman::Client
    Distributed::Gearman::Worker

      i just change the way the client work, it is gearman which is distributed by itself. also i didn't wrote code for the worker.

        I'm confused. I thought you were looking for better names for Gearman::OO::Client and Gearman::OO::Job.

        On second thought, though, Job or Task is better than Worker. Worker is what executes. The Job or Task is the work unit. Maybe that's what you meant.

        Gearman calls them tasks, so
        Distributed::Gearman::Client
        Distributed::Gearman::Task

Re: Another Gearman perl module?
by Mr. Muskrat (Canon) on Nov 17, 2009 at 23:32 UTC
Re: Another Gearman perl module?
by ikegami (Patriarch) on Nov 16, 2009 at 21:13 UTC
    How about

    dist:
    Distributed-Gearman

    modules:
    Distributed::Gearman::Client
    Distributed::Gearman::Worker

Re: Another Gearman perl module?
by runrig (Abbot) on Apr 05, 2010 at 21:35 UTC
    Why put a limit on the number of queued jobs? Let gearman worry about the number of jobs in the queue. That's what it's for.