Case analysis of priority and return status of tasks in gearman

Time:2021-6-1

This paper describes the priority and return status of tasks in gearman. The details are as follows:

Priority and return status of tasks in gearman

1、 Priority of tasks

Block the call synchronously and wait for the result to be returned

Dolow: lowest priority
Donomal: normal priority
Dohigh: top priority

The asynchronous dispatch task does not wait for the return result, but returns the task handle, through which the task running status information can be obtained


doLowBackground
doBackground
doHighBackground

Note the difference between task and doxxx. Task is a group of jobs, which will be distributed to multiple workers for parallel execution and return the results to the caller.
Each task of doxxx will only be executed on one worker.


addTaskLow
addTask
addTaskHigh

addTaskLowBackground 
addTaskBackground 
addTaskHighBackground

The code of cli.php is as follows:

<?php
$client = new GearmanClient();
$client->addServer('127.0.0.1', 4730);
$client->setCompleteCallback(function($task) {
  echo $task->data(), PHP_EOL;
});
//Add tasks with different priorities
$client->addTaskLow('sum', json_encode(array(100, 100)));
$client->addTask('sum', json_encode(array(200, 200)));
$client->addTaskHigh('sum', json_encode(array(300, 300)));

$client->runTasks();

The code of worker.php is as follows:


<?php
$worker = new GearmanWorker();
$worker->addServer('127.0.0.1', 4730);
$worker->addFunction('sum', function($job) {
  $data = json_decode($job->workload(), true);
  $ret = $data[0] + $data[1];
  echo $ret, PHP_EOL;
  sleep(1);
  return $ret;
});
while($worker->work());

Note that in order to experiment, the worker only needs to start one.

2、 Gets the status of the task

The code of cli.php is as follows:

<?php
$client = new GearmanClient();
$client->addServer('127.0.0.1', 4730);
//Callback when tasks are queued
$client->setCreatedCallback('reverse_create');
//Callback when there are task packets
$client->setDataCallback('reverse_data');
//Collect callbacks for task status
$client->setStatusCallback('reverse_status');
//Callback on task completion
$client->setCompleteCallback('reverse_complete');
//Callback when task fails
$client->setFailCallback('reverse_fail');
//Add tasks with different priorities
$client->addTaskLow('reverse', '1234567');
$client->addTask('reverse', 'hello');
$client->addTaskHigh('reverse', 'world');

$client->runTasks();

function reverse_create($task) {
  Echo 'task creation (' $task - > jobhandle(), '):', PHP_ EOL;
}
function reverse_data($task) {
  Echo 'receive data (' $task - > jobhandle(), '):' $task - > data(), PHP_ EOL;
}
function reverse_status($task) {
  //Tasknumerator() gets the numerator of task completion
  //Taskdenominator() gets the denominator of task completion
  Echo 'completion status (' $task - > jobhandle(), '):' $task - > tasknumerator(), '/' $task - > taskdenominator()_ EOL;
}
function reverse_complete($task) {
  Echo 'task completed (' $task - > jobhandle(), '):' $task - > data(), PHP_ EOL;
}
function reverse_fail($task) {
  Echo 'task failed (' $task - > jobhandle(), '):', PHP_ EOL;
}

The code of worker.php is as follows:

<?php
$worker = new GearmanWorker();
$worker->addServer('127.0.0.1', 4730);
$worker->addFunction('reverse', function($job) {
  //Workload () returns the received data
  $data = $job->workload();
  //Workloadsize() returns the byte size of the received data
  $dataSize = $job->workloadSize();
  echo "worker: {$data} ({$dataSize}) \n";

  $tmp = 0;
  $mid = $dataSize / 2;
  for($ix = 0; $ix < $mid; ++$ix) {
    $tmp = $data[$ix];
    $data[$ix] = $data[$dataSize - $ix - 1];
    $data[$dataSize - $ix - 1] = $tmp;

    //Here, the sending status will be called back by the client_ Status() received
    //Numerator and denominator of state completion
    $job->sendStatus($ix + 1, $mid + 1);
    //Send data, reverse_ Data() received
    $job->sendData($data);
    sleep(1);
  }
  return $data;
});
while($worker->work());

The results are as follows

For more information about PHP, readers interested in this site can see the following topics: summary of PHP process and thread operation skills, summary of PHP network programming skills, introductory course of PHP basic syntax, complete collection of PHP array operation skills, summary of PHP string usage “PHP + MySQL database operation tutorial” and “PHP common database operation skills summary”

I hope this article is helpful for PHP programming.

Recommended Today

The principle and practice of redis cluster ha (the most complete in history and necessary for interview)

The article is very long. I suggest you collect it and read it slowly!General catalog of crazy maker circle YuQue Edition | General directory code cloud version| General directory blog Garden EditionPrecious learning resources for you: Free classic books:Java high concurrency core programming (Volume 1)Necessary for interview + necessary for large enterprises + necessary for […]