whats the difference between a task and do whats the difference between a task and do php php

whats the difference between a task and do


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() orGearmanClient::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.


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.