Gearman GearmanWorker::addFunction

(PECL gearman >= 0.5.0)

GearmanWorker::addFunction — 콜백 함수 등록 및 추가


설명

public GearmanWorker::addFunction(
    string $function_name,
    callable $function,
    mixed &$context = ?,
    int $timeout = ?
): bool
                

작업 서버에 함수 이름을 등록하고 해당 함수에 해당하는 콜백을 지정합니다. 선택적으로 콜백이 호출되고 시간 초과될 때 사용할 추가 애플리케이션 컨텍스트 데이터를 지정합니다.


매개변수

function_name
작업 서버에 등록할 함수의 이름
function
등록된 함수 이름에 대한 작업이 제출될 때 호출되는 콜백
context
작업자 함수로 수정할 수 있는 임의의 애플리케이션 컨텍스트 데이터에 대한 참조
timeout
시간 간격(초)

반환 값

성공하면 true를, 실패하면 false를 반환합니다.


Examples

예제 #1 추가 애플리케이션 컨텍스트 데이터를 사용하는 간단한 작업자

                  
<?php

# get a gearman worker
$worker= new GearmanWorker();

# add the default server (localhost)
$worker->addServer();

# define a variable to hold application data
$count= 0;

# add the "reverse" function
$worker->addFunction("reverse", "reverse_cb", $count);

# start the worker
while ($worker->work());

function reverse_cb($job, &$count)
{
  $count++;
  return "$count: " . strrev($job->workload());
}

?>
                  
                

역기능에 대해 두 개의 작업을 제출하는 클라이언트를 실행하면 다음과 유사한 출력이 표시됩니다.

1: olleh
2: dlrow
                

기타

  • GearmanClient::do() - 단일 작업을 실행하고 결과를 반환합니다. [더 이상 사용되지 않음]