Gearman GearmanClient::addTask

(PECL gearman >= 0.5.0)

GearmanClient::addTask — 병렬로 실행할 작업 추가


설명

public GearmanClient::addTask(
    string $function_name,
    string $workload,
    mixed &$context = ?,
    string $unique = ?
): GearmanTask
                

다른 작업과 병렬로 실행할 작업을 추가합니다. 모든 작업이 병렬로 실행되도록 이 메서드를 호출한 다음 GearmanClient::runTasks()를 호출하여 작업을 수행합니다. 작업을 모두 병렬로 실행하려면 충분한 작업자를 사용할 수 있어야 합니다.


매개변수

function_name
작업자가 실행할 등록된 함수
workload
처리할 직렬화된 데이터
context
작업과 연결할 애플리케이션 컨텍스트
unique
특정 작업을 식별하는 데 사용되는 고유 ID

반환 값

작업을 추가할 수 없는 경우 GearmanTask 개체 또는 false입니다.


Examples

예제 #1 두 가지 작업의 기본 제출

                  
<?php

# Create our gearman client
$gmclient= new GearmanClient();

# add the default job server
$gmclient->addServer();

# set a function to be called when the work is complete
$gmclient->setCompleteCallback("complete");

# add a task to perform the "reverse" function on the string "Hello World!"
$gmclient->addTask("reverse", "Hello World!", null, "1");

# add another task to perform the "reverse" function on the string "!dlroW olleH"
$gmclient->addTask("reverse", "!dlroW olleH", null, "2");

# run the tasks
$gmclient->runTasks();

function complete($task)
{
  print "COMPLETE: " . $task->unique() . ", " . $task->data() . "\n";
}

?>
                  
                

위의 예는 다음과 유사한 결과를 출력합니다.

COMPLETE: 2, Hello World!
COMPLETE: 1, !dlroW olleH
                

예제 #2 애플리케이션 컨텍스트를 전달하는 두 가지 작업의 기본 제출

                  
<?php

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

# set a function to be called when the work is complete
$client->setCompleteCallback("reverse_complete");

# Add some tasks for a placeholder of where to put the results
$results = array();
$client->addTask("reverse", "Hello World!", $results, "t1");
$client->addTask("reverse", "!dlroW olleH", $results, "t2");

$client->runTasks();

# The results should now be filled in from the callbacks
foreach ($results as $id => $result)
   echo $id . ": " . $result['handle'] . ", " . $result['data'] . "\n";


function reverse_complete($task, $results)
{
   $results[$task->unique()] = array("handle"=>$task->jobHandle(), "data"=>$task->data());
}

?>
                  
                

위의 예는 다음과 유사한 결과를 출력합니다.

t2: H.foo:21, Hello World!
t1: H:foo:22, !dlroW olleH
                

기타