什么是任务和做什么之间的区别

时间:2021-12-11 21:19:56

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.

当查看php gearman文档时,我看到有任务和做,他们都有背景和非背景,他们也都有高低和正常。

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

谁能澄清这些?我真的很困惑这个区别。

2 个解决方案

#1


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:

运行顺序 - 当您通过do和runTasks运行某些​​任务时,确实具有比任务更高的优先级。所以运行顺序将通过:

  1. all do with hight priority (GearmanClient::doHigh() or
    GearmanClient::doHighBackground())
  2. 所有做的都是高优先级(GearmanClient :: doHigh()或GearmanClient :: doHighBackground())

  3. all task with hight priority (GearmanClient::addTaskHigh() or GearmanClient::addTaskHighBackground())
  4. 所有具有高优先级的任务(GearmanClient :: addTaskHigh()或GearmanClient :: addTaskHighBackground())

  5. all do with normal priority
  6. 一切都做正常的优先事项

  7. all task with normal priority
  8. 所有具有正常优先级的任务

  9. all do with low priority
  10. 都是低优先级

  11. all task with low priority
  12. 所有低优先级的任务

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:

做 - 用于复杂的工作或需要检查进度时。为此目的有GearmanJob :: sendStatus():

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.

$ job_handle是字符串,所以你可以将它存储在某个地方,然后随时检查它。

#2


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

如果你运行它会立即运行(发送到作业服务器)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.

运行单个任务并返回结果的字符串表示形式。由GearmanClient和GearmanWorker决定结果的格式。

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

如果是任务,您可以构建它们的列表,然后在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.

添加要与其他任务并行运行的任务。为所有要并行运行的任务调用此方法,然后调用GearmanClient :: runTasks()来执行工作。请注意,需要有足够的工作人员来完成所有并行运行的任务。

#1


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:

运行顺序 - 当您通过do和runTasks运行某些​​任务时,确实具有比任务更高的优先级。所以运行顺序将通过:

  1. all do with hight priority (GearmanClient::doHigh() or
    GearmanClient::doHighBackground())
  2. 所有做的都是高优先级(GearmanClient :: doHigh()或GearmanClient :: doHighBackground())

  3. all task with hight priority (GearmanClient::addTaskHigh() or GearmanClient::addTaskHighBackground())
  4. 所有具有高优先级的任务(GearmanClient :: addTaskHigh()或GearmanClient :: addTaskHighBackground())

  5. all do with normal priority
  6. 一切都做正常的优先事项

  7. all task with normal priority
  8. 所有具有正常优先级的任务

  9. all do with low priority
  10. 都是低优先级

  11. all task with low priority
  12. 所有低优先级的任务

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:

做 - 用于复杂的工作或需要检查进度时。为此目的有GearmanJob :: sendStatus():

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.

$ job_handle是字符串,所以你可以将它存储在某个地方,然后随时检查它。

#2


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

如果你运行它会立即运行(发送到作业服务器)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.

运行单个任务并返回结果的字符串表示形式。由GearmanClient和GearmanWorker决定结果的格式。

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

如果是任务,您可以构建它们的列表,然后在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.

添加要与其他任务并行运行的任务。为所有要并行运行的任务调用此方法,然后调用GearmanClient :: runTasks()来执行工作。请注意,需要有足够的工作人员来完成所有并行运行的任务。