13

When looking inside the php gearman docs I see that there are task and do, both of them have background and non background and they also all have high and low and normal.

Can anyone clarify these? I'm just really confused about the difference.

NDM
  • 6,731
  • 3
  • 39
  • 52
WojonsTech
  • 1,277
  • 1
  • 13
  • 28
  • what you want to do with it?? – Yogesh Suthar Oct 02 '12 at 11:18
  • 2
    Well i was told that its better then setting cron jobs all the time. but i am trying to find the differance and if one is better than the other and kinda of how to use it i see lots of examples in online but having a hard time trying get the differance between the two. – WojonsTech Oct 02 '12 at 11:20

2 Answers2

8

There are two differences: running order and purpose.

Running order - when you run some tasks by do and by runTasks then do has higher priority than tasks. So the running order will by:

  1. all do with hight priority (GearmanClient::doHigh() or
    GearmanClient::doHighBackground())
  2. all task with hight priority (GearmanClient::addTaskHigh() or GearmanClient::addTaskHighBackground())
  3. all do with normal priority
  4. all task with normal priority
  5. all do with low priority
  6. all task with low priority

Purpose:

Task - use this for short tasks, when you do not care when it finish or how is progress

Do - use this for complex job or when you need to check progress. There is GearmanJob::sendStatus() for this purpose:

worker.php

$worker = new GearmanWorker();
$worker->addServer();
$worker->addFunction("sleep13", array('MyWorker', 'sleep13'));
while ($worker->work());

class MyWorker {
    public function sleep13($job) {
        $data = unserialize($job->workload());
        echo 'start ' . $data['id']  . PHP_EOL;

        for($i = 0; $i < 13; $i++) {
            sleep(1);
            $job->sendStatus($i, 13);
        }
        echo 'done ' . $data['id']  . PHP_EOL;
    }
}

client.php

$client = new GearmanClient();
$client->addServer();

// Run task
$job_handle = $client->doBackground("sleep13", serialize(array('id' => 'normal-1')));

// Check progress
$done = false;
do {
   usleep(300);
   $stat = $client->jobStatus($job_handle);
   if (!$stat[0]) // the job is known so it is not done
      $done = true;
   echo "Running: " . ($stat[1] ? "true" : "false") . ", numerator: " . $stat[2] . ", denomintor: " . $stat[3] . "\n";
} while(!$done);

echo "done!\n";

$job_handle is string so you can store it somewhere and then check it anytime.

  • 1
    Whats the difference between doBackground and doNormal? I need to have the worker return data back to the client. – CMCDragonkai Oct 01 '13 at 04:12
  • 2
    doBackground causes the client to not wait for the job to finish and returns immediately. you can check on the status later asyncronously. doNormal forces the client to wait for the workers response. – David Chan Oct 18 '13 at 05:37
7

Well I have done some research for you as I have thought about this too.

If you run a do it runs that straight away (sends to job server) http://www.php.net/manual/en/gearmanclient.donormal.php

Do

Runs a single task and returns a string representation of the result. It is up to the GearmanClient and GearmanWorker to agree on the format of the result.

Were task you can build a list of them and then run them Parallel when you GearmanClient::Run().

http://www.php.net/manual/en/gearmanclient.addtask.php

Task

Adds a task to be run in parallel with other tasks. Call this method for all the tasks to be run in parallel, then call GearmanClient::runTasks() to perform the work. Note that enough workers need to be available for the tasks to all run in parallel.

Simon Bennett
  • 355
  • 3
  • 8
  • there does seem to be dobackground tho http://www.php.net/manual/en/gearmanclient.dobackground.php – WojonsTech Oct 08 '12 at 10:40
  • Yes they do, I just did the edit to make the two links more comparable. Both 'do' as 'task' have functions for different levels of priority and weather you want background or not. – Simon Bennett Oct 08 '12 at 12:19