Book/gearman-Phpdoc专题

Gearman

目录

简介

Represents a class for connecting to a Gearman job server and making requests to perform some function on provided data. The function performed must be one registered by a Gearman worker and the data passed is opaque to the job server.

类摘要

GearmanClient

class GearmanClient {

/* 方法 */

public bool addOptions ( <span class="methodparam">int $options )

public bool addServer ([ <span class="methodparam">string $host<span class="initializer"> = 127.0.0.1 [, <span class="methodparam">int $port<span class="initializer"> = 4730 ]] )

public bool addServers ([ <span class="methodparam">string $servers<span class="initializer"> = 127.0.0.1:4730 ] )

public <span class="type">GearmanTask addTask ( string $function_name , <span class="type">string $workload [, <span class="methodparam">mixed &$context [, string $unique ]] )

public <span class="type">GearmanTask <span class="methodname">addTaskBackground ( <span class="methodparam">string $function_name , <span class="type">string $workload [, <span class="methodparam">mixed &$context [, string $unique ]] )

public <span class="type">GearmanTask <span class="methodname">addTaskHigh ( <span class="type">string $function_name , <span class="methodparam">string $workload [, mixed &$context [, <span class="type">string $unique ]] )

public <span class="type">GearmanTask <span class="methodname">addTaskHighBackground ( <span class="methodparam">string $function_name , <span class="type">string $workload [, <span class="methodparam">mixed &$context [, string $unique ]] )

public <span class="type">GearmanTask <span class="methodname">addTaskLow ( <span class="type">string $function_name , <span class="methodparam">string $workload [, mixed &$context [, <span class="type">string $unique ]] )

public <span class="type">GearmanTask <span class="methodname">addTaskLowBackground ( <span class="methodparam">string $function_name , <span class="type">string $workload [, <span class="methodparam">mixed &$context [, string $unique ]] )

public <span class="type">GearmanTask <span class="methodname">addTaskStatus ( <span class="methodparam">string $job_handle [, <span class="type">string &$context ] )

public bool clearCallbacks ( <span class="methodparam">void )

public <span class="type">GearmanClient clone ( void )

public <span class="methodname">__construct ( <span class="methodparam">void )

public string context ( <span class="methodparam">void )

public string data ( <span class="methodparam">void )

public string do ( <span class="type">string $function_name , <span class="methodparam">string $workload [, string $unique ] )

public string doBackground ( <span class="methodparam">string $function_name , <span class="type">string $workload [, <span class="methodparam">string $unique ] )

public string doHigh ( <span class="type">string $function_name , <span class="methodparam">string $workload [, string $unique ] )

public string doHighBackground ( <span class="methodparam">string $function_name , <span class="type">string $workload [, <span class="methodparam">string $unique ] )

public string doJobHandle ( <span class="methodparam">void )

public string doLow ( <span class="type">string $function_name , <span class="methodparam">string $workload [, string $unique ] )

public string doLowBackground ( <span class="methodparam">string $function_name , <span class="type">string $workload [, <span class="methodparam">string $unique ] )

public string doNormal ( <span class="methodparam">string $function_name , <span class="type">string $workload [, <span class="methodparam">string $unique ] )

public array doStatus ( <span class="methodparam">void )

public bool echo ( <span class="type">string $workload )

public string error ( <span class="methodparam">void )

public int <span class="methodname">getErrno ( <span class="methodparam">void )

public array jobStatus ( <span class="methodparam">string $job_handle )

public bool ping ( <span class="type">string $workload )

public bool removeOptions ( <span class="methodparam">int $options )

public int <span class="methodname">returnCode ( <span class="methodparam">void )

public bool runTasks ( <span class="methodparam">void )

public void setClientCallback ( <span class="methodparam">callable $callback )

public bool setCompleteCallback ( <span class="methodparam">callable $callback )

public bool setContext ( <span class="methodparam">string $context )

public bool setCreatedCallback ( <span class="methodparam">string $callback )

public bool setData ( <span class="methodparam">string $data )

public bool setDataCallback ( <span class="methodparam">callable $callback )

public bool setExceptionCallback ( <span class="methodparam">callable $callback )

public bool setFailCallback ( <span class="methodparam">callable $callback )

public bool setOptions ( <span class="methodparam">int $options )

public bool setStatusCallback ( <span class="methodparam">callable $callback )

public bool setTimeout ( <span class="methodparam">int $timeout )

public bool setWarningCallback ( <span class="methodparam">callable $callback )

public bool setWorkloadCallback ( <span class="methodparam">callable $callback )

public int <span class="methodname">timeout ( <span class="methodparam">void )

}

GearmanClient::addOptions

Add client options

说明

public bool GearmanClient::addOptions ( <span class="methodparam">int $options )

Adds one or more options to those already set.

参数

options
The options to add. One of the following constants, or a combination of them using the bitwise OR operator (“|”): GEARMAN_CLIENT_GENERATE_UNIQUE, GEARMAN_CLIENT_NON_BLOCKING, GEARMAN_CLIENT_UNBUFFERED_RESULT or GEARMAN_CLIENT_FREE_TASKS.

返回值

Always returns true.

GearmanClient::addServer

Add a job server to the client

说明

public bool GearmanClient::addServer ([ <span class="methodparam">string $host<span class="initializer"> = 127.0.0.1 [, <span class="methodparam">int $port<span class="initializer"> = 4730 ]] )

Adds a job server to a list of servers that can be used to run a task. No socket I/O happens here; the server is simply added to the list.

参数

host
任务服务器主机名。

port
任务服务器端口号。

返回值

成功时返回 true, 或者在失败时返回 false

范例

示例 #1 Adding two job servers

<?php

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

# Add two job servers, the first on the default 4730 port
$gmclient->addServer("10.0.0.1"); 
$gmclient->addServer("10.0.0.2", 7003);

?>

参见

  • GearmanClient::addServers

GearmanClient::addServers

Add a list of job servers to the client

说明

public bool GearmanClient::addServers ([ <span class="methodparam">string $servers<span class="initializer"> = 127.0.0.1:4730 ] )

Adds a list of job servers that can be used to run a task. No socket I/O happens here; the servers are simply added to the full list of servers.

参数

servers
A comma-separated list of servers, each server specified in the format 'host:port'.

返回值

成功时返回 true, 或者在失败时返回 false

范例

示例 #1 Add two job servers

<?php

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

# Add multiple job servers, the first on the default 4730 port
$gmclient->addServers("10.0.0.1,10.0.0.2:7003");

?>

参见

  • GearmanClient::addServer

GearmanClient::addTask

Add a task to be run in parallel

说明

public <span class="type">GearmanTask <span class="methodname">GearmanClient::addTask ( <span class="methodparam">string $function_name , <span class="type">string $workload [, <span class="methodparam">mixed &$context [, string $unique ]] )

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 <span class="methodname">GearmanClient::runTasks to perform the work. Note that enough workers need to be available for the tasks to all run in parallel.

参数

function_name
由程序自动执行的已注册函数。

workload
被处理的序列化数据。

context
与任务关联的应用程序上下文。

unique
用于标识特定任务的唯一性 ID。

返回值

A GearmanTask object or false if the task could not be added.

范例

示例 #1 Basic submission of two tasks

<?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 Basic submission of two tasks with passing application context

<?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

参见

  • GearmanClient::addTaskHigh
  • GearmanClient::addTaskLow
  • GearmanClient::addTaskBackground
  • GearmanClient::addTaskHighBackground
  • GearmanClient::addTaskLowBackground
  • GearmanClient::runTasks

GearmanClient::addTaskBackground

Add a background task to be run in parallel

说明

public <span class="type">GearmanTask <span class="methodname">GearmanClient::addTaskBackground ( <span class="methodparam">string $function_name , <span class="type">string $workload [, <span class="methodparam">mixed &$context [, string $unique ]] )

Adds a background task to be run in parallel with other tasks. Call this method for all the tasks to be run in parallel, then call <span class="methodname">GearmanClient::runTasks to perform the work.

参数

function_name
由程序自动执行的已注册函数。

workload
被处理的序列化数据。

context
与任务关联的应用程序上下文。

unique
用于标识特定任务的唯一性 ID。

返回值

A GearmanTask object or false if the task could not be added.

范例

示例 #1 Two tasks, one background and one not

This example illustrates the difference between running a background task and a normal task. The client adds two tasks to execute the same function, but one is added with <span class="methodname">addTaskBackground. A callback is set so that progress of the job can be tracked. A simple worker with an artificial delay reports on the job progress and the client picks this up through the callback. Two workers are run for this example. Note that the background task does not show in the client output.

<?php

# The client script

# create our gearman client
$gmc= new GearmanClient();

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

# set a couple of callbacks so we can track progress
$gmc->setCompleteCallback("reverse_complete");
$gmc->setStatusCallback("reverse_status");

# add a task for the "reverse" function
$task= $gmc->addTask("reverse", "Hello World!", null, "1");

# add another task, but this one to run in the background
$task= $gmc->addTaskBackground("reverse", "!dlroW olleH", null, "2");

if (! $gmc->runTasks())
{
    echo "ERROR " . $gmc->error() . "\n";
    exit;
}

echo "DONE\n";

function reverse_status($task)
{
    echo "STATUS: " . $task->unique() . ", " . $task->jobHandle() . " - " . $task->taskNumerator() . 
         "/" . $task->taskDenominator() . "\n";
}

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

?>
<?php

# The worker script

echo "Starting\n";

# Create our worker object.
$gmworker= new GearmanWorker();

# Add default server (localhost).
$gmworker->addServer();

# Register function "reverse" with the server.
$gmworker->addFunction("reverse", "reverse_fn");

print "Waiting for job...\n";
while($gmworker->work())
{
  if ($gmworker->returnCode() != GEARMAN_SUCCESS)
  {
    echo "return_code: " . $gmworker->returnCode() . "\n";
    break;
  }
}

function reverse_fn($job)
{
  echo "Received job: " . $job->handle() . "\n";

  $workload = $job->workload();
  $workload_size = $job->workloadSize();

  echo "Workload: $workload ($workload_size)\n";

  # This status loop is not needed, just showing how it works
  for ($x= 0; $x < $workload_size; $x++)
  {
    echo "Sending status: " . ($x + 1) . "/$workload_size complete\n";
    $job->sendStatus($x+1, $workload_size);
    $job->sendData(substr($workload, $x, 1));
    sleep(1);
  }

  $result= strrev($workload);
  echo "Result: $result\n";

  # Return what we want to send back to the client.
  return $result;
}

?>

Worker output for two workers running:

Received job: H:foo.local:65
Workload: !dlroW olleH (12)
1/12 complete
Received job: H:foo.local:66
Workload: Hello World! (12)
Sending status: 1/12 complete
Sending status: 2/12 complete
Sending status: 2/12 complete
Sending status: 3/12 complete
Sending status: 3/12 complete
Sending status: 4/12 complete
Sending status: 4/12 complete
Sending status: 5/12 complete
Sending status: 5/12 complete
Sending status: 6/12 complete
Sending status: 6/12 complete
Sending status: 7/12 complete
Sending status: 7/12 complete
Sending status: 8/12 complete
Sending status: 8/12 complete
Sending status: 9/12 complete
Sending status: 9/12 complete
Sending status: 10/12 complete
Sending status: 10/12 complete
Sending status: 11/12 complete
Sending status: 11/12 complete
Sending status: 12/12 complete
Sending status: 12/12 complete
Result: !dlroW olleH
Result: Hello World!

Client output:

STATUS: 1, H:foo.local:66 - 1/12
STATUS: 1, H:foo.local:66 - 2/12
STATUS: 1, H:foo.local:66 - 3/12
STATUS: 1, H:foo.local:66 - 4/12
STATUS: 1, H:foo.local:66 - 5/12
STATUS: 1, H:foo.local:66 - 6/12
STATUS: 1, H:foo.local:66 - 7/12
STATUS: 1, H:foo.local:66 - 8/12
STATUS: 1, H:foo.local:66 - 9/12
STATUS: 1, H:foo.local:66 - 10/12
STATUS: 1, H:foo.local:66 - 11/12
STATUS: 1, H:foo.local:66 - 12/12
COMPLETE: 1, !dlroW olleH
DONE

参见

  • GearmanClient::addTask
  • GearmanClient::addTaskHigh
  • GearmanClient::addTaskLow
  • GearmanClient::addTaskHighBackground
  • GearmanClient::addTaskLowBackground
  • GearmanClient::runTasks

GearmanClient::addTaskHigh

Add a high priority task to run in parallel

说明

public <span class="type">GearmanTask <span class="methodname">GearmanClient::addTaskHigh ( <span class="methodparam">string $function_name , <span class="type">string $workload [, <span class="methodparam">mixed &$context [, string $unique ]] )

Adds a high priority task to be run in parallel with other tasks. Call this method for all the high priority tasks to be run in parallel, then call GearmanClient::runTasks to perform the work. Tasks with a high priority will be selected from the queue before those of normal or low priority.

参数

function_name
由程序自动执行的已注册函数。

workload
被处理的序列化数据。

context
与任务关联的应用程序上下文。

unique
用于标识特定任务的唯一性 ID。

返回值

A GearmanTask object or false if the task could not be added.

范例

示例 #1 A high priority task along with two normal tasks

A high priority task is included among two other tasks. A single worker is available, so that tasks are run one at a time, with the high priority task run first.

<?php

# create the gearman client
$gmc= new GearmanClient();

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

# set the callback for when the job is complete
$gmc->setCompleteCallback("reverse_complete");

# add tasks, one of which is high priority
$task= $gmc->addTask("reverse", "Hello World!", null, "1");
$task= $gmc->addTaskHigh("reverse", "!dlroW olleH", null, "2");
$task= $gmc->addTask("reverse", "Hello World!", null, "3");

if (! $gmc->runTasks())
{
    echo "ERROR " . $gmc->error() . "\n";
    exit;
}
echo "DONE\n";

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

?>

以上例程的输出类似于:

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

参见

  • GearmanClient::addTask
  • GearmanClient::addTaskLow
  • GearmanClient::addTaskBackground
  • GearmanClient::addTaskHighBackground
  • GearmanClient::addTaskLowBackground
  • GearmanClient::runTasks

GearmanClient::addTaskHighBackground

Add a high priority background task to be run in parallel

说明

public <span class="type">GearmanTask <span class="methodname">GearmanClient::addTaskHighBackground ( <span class="methodparam">string $function_name , <span class="type">string $workload [, <span class="methodparam">mixed &$context [, string $unique ]] )

Adds a high priority background 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. Tasks with a high priority will be selected from the queue before those of normal or low priority.

参数

function_name
由程序自动执行的已注册函数。

workload
被处理的序列化数据。

context
与任务关联的应用程序上下文。

unique
用于标识特定任务的唯一性 ID。

返回值

A GearmanTask object or false if the task could not be added.

参见

  • GearmanClient::addTask
  • GearmanClient::addTaskHigh
  • GearmanClient::addTaskLow
  • GearmanClient::addTaskBackground
  • GearmanClient::addTaskLowBackground
  • GearmanClient::runTasks

GearmanClient::addTaskLow

Add a low priority task to run in parallel

说明

public <span class="type">GearmanTask <span class="methodname">GearmanClient::addTaskLow ( <span class="methodparam">string $function_name , <span class="type">string $workload [, <span class="methodparam">mixed &$context [, string $unique ]] )

Adds a low priority background 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. Tasks with a low priority will be selected from the queue after those of normal or low priority.

参数

function_name
由程序自动执行的已注册函数。

workload
被处理的序列化数据。

context
与任务关联的应用程序上下文。

unique
用于标识特定任务的唯一性 ID。

返回值

A GearmanTask object or false if the task could not be added.

范例

示例 #1 A low priority task along with two normal tasks

A low priority task is included among two other tasks. A single worker is available, so that tasks are run one at a time, with the low priority task run last.

<?php

# create the gearman client
$gmc= new GearmanClient();

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

# set the callback for when the job is complete
$gmc->setCompleteCallback("reverse_complete");

# add tasks, one of which is low priority
$task= $gmc->addTask("reverse", "Hello World!", null, "1");
$task= $gmc->addTaskLow("reverse", "!dlroW olleH", null, "2");
$task= $gmc->addTask("reverse", "Hello World!", null, "3");

if (! $gmc->runTasks())
{
    echo "ERROR " . $gmc->error() . "\n";
    exit;
}
echo "DONE\n";

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

?>

以上例程的输出类似于:

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

参见

  • GearmanClient::addTask
  • GearmanClient::addTaskHigh
  • GearmanClient::addTaskBackground
  • GearmanClient::addTaskHighBackground
  • GearmanClient::addTaskLowBackground
  • GearmanClient::runTasks

GearmanClient::addTaskLowBackground

Add a low priority background task to be run in parallel

说明

public <span class="type">GearmanTask <span class="methodname">GearmanClient::addTaskLowBackground ( <span class="methodparam">string $function_name , <span class="type">string $workload [, <span class="methodparam">mixed &$context [, string $unique ]] )

Adds a low priority background 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. Tasks with a low priority will be selected from the queue after those of normal or high priority.

参数

function_name
由程序自动执行的已注册函数。

workload
被处理的序列化数据。

context
与任务关联的应用程序上下文。

unique
用于标识特定任务的唯一性 ID。

返回值

A GearmanTask object or false if the task could not be added.

参见

  • GearmanClient::addTask
  • GearmanClient::addTaskHigh
  • GearmanClient::addTaskLow
  • GearmanClient::addTaskBackground
  • GearmanClient::addTaskHighBackground
  • GearmanClient::runTasks

GearmanClient::addTaskStatus

Add a task to get status

说明

public <span class="type">GearmanTask <span class="methodname">GearmanClient::addTaskStatus ( <span class="methodparam">string $job_handle [, <span class="type">string &$context ] )

Used to request status information from the Gearman server, which will call the specified status callback (set using <span class="methodname">GearmanClient::setStatusCallback).

参数

job_handle
The job handle for the task to get status for

context
Data to be passed to the status callback, generally a reference to an array or object

返回值

A GearmanTask object.

范例

示例 #1 Monitor completion of multiple background tasks

An artificial delay is introduced in the worker in this example to simulate a long running process. There is only one worker running for this example.

<?php

/* create our object */
$gmclient= new GearmanClient();

/* add the default server */
$gmclient->addServer();

/* start some background jobs and save the handles */
$handles = array();
$handles[0] = $gmclient->doBackground("reverse", "Hello World!");
$handles[1] = $gmclient->doBackground("reverse", "!dlroW olleH");

$gmclient->setStatusCallback("reverse_status");

/* Poll the server to see when those background jobs finish; */
/* a better method would be to use event callbacks */
do
{
   /* Use the context variable to track how many tasks have completed */
   $done = 0;
   $gmclient->addTaskStatus($handles[0], &$done);
   $gmclient->addTaskStatus($handles[1], &$done);
   $gmclient->runTasks();
   echo "Done: $done\n";
   sleep(1);
}
while ($done != 2);

function reverse_status($task, $done)
{
   if (!$task->isKnown())
      $done++;
}

?>

以上例程的输出类似于:

Done: 0
Done: 0
Done: 0
Done: 0
Done: 0
Done: 0
Done: 0
Done: 0
Done: 0
Done: 0
Done: 0
Done: 0
Done: 1
Done: 1
Done: 1
Done: 1
Done: 1
Done: 1
Done: 1
Done: 1
Done: 1
Done: 1
Done: 1
Done: 1
Done: 2

参见

  • GearmanClient::setStatusCallback

GearmanClient::clearCallbacks

Clear all task callback functions

说明

public bool GearmanClient::clearCallbacks ( <span class="methodparam">void )

Clears all the task callback functions that have previously been set.

参数

此函数没有参数。

返回值

Always returns true.

参见

  • GearmanClient::setDataCallback
  • GearmanClient::setCompleteCallback
  • GearmanClient::setCreatedCallback
  • GearmanClient::setExceptionCallback
  • GearmanClient::setFailCallback
  • GearmanClient::setStatusCallback
  • GearmanClient::setWarningCallback
  • GearmanClient::setWorkloadCallback

GearmanClient::clone

Create a copy of a GearmanClient object

说明

public <span class="type">GearmanClient <span class="methodname">GearmanClient::clone ( <span class="methodparam">void )

Creates a copy of a GearmanClient object.

参数

此函数没有参数。

返回值

A GearmanClient on success, false on failure.

GearmanClient::__construct

Create a GearmanClient instance

说明

public <span class="methodname">GearmanClient::__construct ( <span class="methodparam">void )

Creates a GearmanClient instance representing a client that connects to the job server and submits tasks to complete.

参数

此函数没有参数。

返回值

A GearmanClient object.

参见

  • GearmanClient::clone

GearmanClient::context

Get the application context

说明

public string GearmanClient::context ( <span class="methodparam">void )

Get the application context previously set with <span class="methodname">GearmanClient::setContext.

参数

此函数没有参数。

返回值

The same context data structure set with <span class="methodname">GearmanClient::setContext

参见

  • GearmanClient::setContext

GearmanClient::data

Get the application data (deprecated)

说明

public string GearmanClient::data ( <span class="methodparam">void )

Get the application data previously set with <span class="methodname">GearmanClient::setData.

Note:

This method was replaced by <span class="methodname">GearmanClient::setContext in the 0.6.0 release of the Gearman extension.

参数

此函数没有参数。

返回值

The same string data set with <span class="methodname">GearmanClient::setData

参见

  • GearmanClient::setData

GearmanClient::do

Run a single task and return a result [deprecated]

说明

public string GearmanClient::do ( <span class="methodparam">string $function_name , <span class="type">string $workload [, <span class="methodparam">string $unique ] )

The GearmanClient::do method is deprecated as of pecl/gearman 1.0.0. Use <span class="methodname">GearmanClient::doNormal.

参数

function_name
由程序自动执行的已注册函数。

workload
被处理的序列化数据。

unique
用于标识特定任务的唯一性 ID。

返回值

A string representing the results of running a task.

范例

示例 #1 Simple job submission with immediate return

<?php

# Client code

echo "Starting\n";

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

# Add default server (localhost).
$gmclient->addServer();

echo "Sending job\n";

$result = $gmclient->doNormal("reverse", "Hello!");

echo "Success: $result\n";

?>
<?php

echo "Starting\n";

# Create our worker object.
$gmworker= new GearmanWorker();

# Add default server (localhost).
$gmworker->addServer();

# Register function "reverse" with the server. Change the worker function to
# "reverse_fn_fast" for a faster worker with no output.
$gmworker->addFunction("reverse", "reverse_fn");

print "Waiting for job...\n";
while($gmworker->work())
{
  if ($gmworker->returnCode() != GEARMAN_SUCCESS)
  {
    echo "return_code: " . $gmworker->returnCode() . "\n";
    break;
  }
}

function reverse_fn($job)
{
  return strrev($job->workload());
}

?>

以上例程的输出类似于:

Starting
Sending job
Success: !olleH

示例 #2 Submitting a job and retrieving incremental status

A job is submitted and the script loops to retrieve status information. The worker has an artificial delay which results in a long running job and sends status and data as processing occurs. Each subsequent call to GearmanClient::do produces status information on the running job.

<?php

# Client code

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

# Add default server (localhost).
$gmclient->addServer();

echo "Sending job\n";

# Send reverse job
do
{
  $result = $gmclient->doNormal("reverse", "Hello!");
  # Check for various return packets and errors.

  switch($gmclient->returnCode())
  {
    case GEARMAN_WORK_DATA:
      echo "Data: $result\n";
      break;
    case GEARMAN_WORK_STATUS:
      list($numerator, $denominator)= $gmclient->doStatus();
      echo "Status: $numerator/$denominator complete\n";
      break;
    case GEARMAN_WORK_FAIL:
      echo "Failed\n";
      exit;
    case GEARMAN_SUCCESS:
      break;
    default:
      echo "RET: " . $gmclient->returnCode() . "\n";
      echo "Error: " . $gmclient->error() . "\n";
      echo "Errno: " . $gmclient->getErrno() . "\n";
      exit;
  }
}
while($gmclient->returnCode() != GEARMAN_SUCCESS);

echo "Success: $result\n";

?>
<?php

# Worker code

echo "Starting\n";

# Create our worker object.
$gmworker= new GearmanWorker();

# Add default server (localhost).
$gmworker->addServer();

# Register function "reverse" with the server.
$gmworker->addFunction("reverse", "reverse_fn");

print "Waiting for job...\n";
while($gmworker->work())
{
  if ($gmworker->returnCode() != GEARMAN_SUCCESS)
  {
    echo "return_code: " . $gmworker->returnCode() . "\n";
    break;
  }
}

function reverse_fn($job)
{
  echo "Received job: " . $job->handle() . "\n";

  $workload = $job->workload();
  $workload_size = $job->workloadSize();

  echo "Workload: $workload ($workload_size)\n";

  # This status loop is not needed, just showing how it works
  for ($x= 0; $x < $workload_size; $x++)
  {
    echo "Sending status: " + $x + 1 . "/$workload_size complete\n";
    $job->sendStatus($x+1, $workload_size);
    $job->sendData(substr($workload, $x, 1));
    sleep(1);
  }

  $result= strrev($workload);
  echo "Result: $result\n";

  # Return what we want to send back to the client.
  return $result;
}

?>

以上例程的输出类似于:

Worker output:

Starting
Waiting for job...
Received job: H:foo.local:106
Workload: Hello! (6)
1/6 complete
2/6 complete
3/6 complete
4/6 complete
5/6 complete
6/6 complete
Result: !olleH

Client output:

Starting
Sending job
Status: 1/6 complete
Data: H
Status: 2/6 complete
Data: e
Status: 3/6 complete
Data: l
Status: 4/6 complete
Data: l
Status: 5/6 complete
Data: o
Status: 6/6 complete
Data: !
Success: !olleH

参见

  • GearmanClient::doHigh
  • GearmanClient::doLow
  • GearmanClient::doBackground
  • GearmanClient::doHighBackground
  • GearmanClient::doLowBackground

GearmanClient::doBackground

Run a task in the background

说明

public string GearmanClient::doBackground ( <span class="methodparam">string $function_name , <span class="type">string $workload [, <span class="methodparam">string $unique ] )

Runs a task in the background, returning a job handle which can be used to get the status of the running task.

参数

function_name
由程序自动执行的已注册函数。

workload
被处理的序列化数据。

unique
用于标识特定任务的唯一性 ID。

返回值

The job handle for the submitted task.

范例

示例 #1 Submit and monitor a background job

The worker in this example has an artificial delay introduced to mimic a long running job. The client script periodically checks the status of the running job.

<?php

/* create our object */
$gmclient= new GearmanClient();

/* add the default server */
$gmclient->addServer();

/* run reverse client */
$job_handle = $gmclient->doBackground("reverse", "this is a test");

if ($gmclient->returnCode() != GEARMAN_SUCCESS)
{
  echo "bad return code\n";
  exit;
}

$done = false;
do
{
   sleep(3);
   $stat = $gmclient->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] . ", denominator: " . $stat[3] . "\n";
}
while(!$done);

echo "done!\n";

?>

以上例程的输出类似于:

Running: true, numerator: 3, denominator: 14
Running: true, numerator: 6, denominator: 14
Running: true, numerator: 9, denominator: 14
Running: true, numerator: 12, denominator: 14
Running: false, numerator: 0, denominator: 0
done!

参见

  • GearmanClient::doNormal
  • GearmanClient::doHigh
  • GearmanClient::doLow
  • GearmanClient::doHighBackground
  • GearmanClient::doLowBackground

GearmanClient::doHigh

Run a single high priority task

说明

public string GearmanClient::doHigh ( <span class="methodparam">string $function_name , <span class="type">string $workload [, <span class="methodparam">string $unique ] )

Runs a single high priority 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. High priority tasks will get precedence over normal and low priority tasks in the job queue.

参数

function_name
由程序自动执行的已注册函数。

workload
被处理的序列化数据。

unique
用于标识特定任务的唯一性 ID。

返回值

A string representing the results of running a task.

参见

  • GearmanClient::doNormal
  • GearmanClient::doLow
  • GearmanClient::doBackground
  • GearmanClient::doHighBackground
  • GearmanClient::doLowBackground

GearmanClient::doHighBackground

Run a high priority task in the background

说明

public string GearmanClient::doHighBackground ( <span class="methodparam">string $function_name , <span class="type">string $workload [, <span class="methodparam">string $unique ] )

Runs a high priority task in the background, returning a job handle which can be used to get the status of the running task. High priority tasks take precedence over normal and low priority tasks in the job queue.

参数

function_name
由程序自动执行的已注册函数。

workload
被处理的序列化数据。

unique
用于标识特定任务的唯一性 ID。

返回值

The job handle for the submitted task.

参见

  • GearmanClient::doNormal
  • GearmanClient::doHigh
  • GearmanClient::doLow
  • GearmanClient::doBackground
  • GearmanClient::doLowBackground

GearmanClient::doJobHandle

Get the job handle for the running task

说明

public string GearmanClient::doJobHandle ( <span class="methodparam">void )

Gets that job handle for a running task. This should be used between repeated GearmanClient::doNormal calls. The job handle can then be used to get information on the task.

参数

此函数没有参数。

返回值

The job handle for the running task.

参见

  • GearmanClient::jobStatus

GearmanClient::doLow

Run a single low priority task

说明

public string GearmanClient::doLow ( <span class="methodparam">string $function_name , <span class="type">string $workload [, <span class="methodparam">string $unique ] )

Runs a single low priority 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. Normal and high priority tasks will get precedence over low priority tasks in the job queue.

参数

function_name
由程序自动执行的已注册函数。

workload
被处理的序列化数据。

unique
用于标识特定任务的唯一性 ID。

返回值

A string representing the results of running a task.

参见

  • GearmanClient::doNormal
  • GearmanClient::doHigh
  • GearmanClient::doBackground
  • GearmanClient::doHighBackground
  • GearmanClient::doLowBackground

GearmanClient::doLowBackground

Run a low priority task in the background

说明

public string GearmanClient::doLowBackground ( <span class="methodparam">string $function_name , <span class="type">string $workload [, <span class="methodparam">string $unique ] )

Runs a low priority task in the background, returning a job handle which can be used to get the status of the running task. Normal and high priority tasks take precedence over low priority tasks in the job queue.

参数

function_name
由程序自动执行的已注册函数。

workload
被处理的序列化数据。

unique
用于标识特定任务的唯一性 ID。

返回值

The job handle for the submitted task.

参见

  • GearmanClient::doNormal
  • GearmanClient::doHigh
  • GearmanClient::doLow
  • GearmanClient::doBackground
  • GearmanClient::doHighBackground

GearmanClient::doNormal

Run a single task and return a result

说明

public string GearmanClient::doNormal ( <span class="methodparam">string $function_name , <span class="type">string $workload [, <span class="methodparam">string $unique ] )

Runs a single task and returns a string representation of the result. It is up to the GearmanClient and <span class="classname">GearmanWorker to agree on the format of the result.

参数

function_name
由程序自动执行的已注册函数。

workload
被处理的序列化数据。

unique
用于标识特定任务的唯一性 ID。

返回值

A string representing the results of running a task.

范例

示例 #1 Simple job submission with immediate return

<?php

?>
<?php

# Client code

echo "Starting\n";

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

# Add default server (localhost).
$gmclient->addServer();

echo "Sending job\n";

$result = $gmclient->doNormal("reverse", "Hello!");

echo "Success: $result\n";

?>
<?php

echo "Starting\n";

# Create our worker object.
$gmworker= new GearmanWorker();

# Add default server (localhost).
$gmworker->addServer();

# Register function "reverse" with the server. Change the worker function to
# "reverse_fn_fast" for a faster worker with no output.
$gmworker->addFunction("reverse", "reverse_fn");

print "Waiting for job...\n";
while($gmworker->work())
{
  if ($gmworker->returnCode() != GEARMAN_SUCCESS)
  {
    echo "return_code: " . $gmworker->returnCode() . "\n";
    break;
  }
}

function reverse_fn($job)
{
  return strrev($job->workload());
}

?>

以上例程的输出类似于:

Starting
Sending job
Success: !olleH

示例 #2 Submitting a job and retrieving incremental status

A job is submitted and the script loops to retrieve status information. The worker has an artificial delay which results in a long running job and sends status and data as processing occurs. Each subsequent call to GearmanClient::doNormal produces status information on the running job.

<?php

# Client code

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

# Add default server (localhost).
$gmclient->addServer();

echo "Sending job\n";

# Send reverse job
do
{
  $result = $gmclient->doNormal("reverse", "Hello!");
  # Check for various return packets and errors.

  switch($gmclient->returnCode())
  {
    case GEARMAN_WORK_DATA:
      echo "Data: $result\n";
      break;
    case GEARMAN_WORK_STATUS:
      list($numerator, $denominator)= $gmclient->doStatus();
      echo "Status: $numerator/$denominator complete\n";
      break;
    case GEARMAN_WORK_FAIL:
      echo "Failed\n";
      exit;
    case GEARMAN_SUCCESS:
      break;
    default:
      echo "RET: " . $gmclient->returnCode() . "\n";
      echo "Error: " . $gmclient->error() . "\n";
      echo "Errno: " . $gmclient->getErrno() . "\n";
      exit;
  }
}
while($gmclient->returnCode() != GEARMAN_SUCCESS);

echo "Success: $result\n";

?>
<?php

# Worker code

echo "Starting\n";

# Create our worker object.
$gmworker= new GearmanWorker();

# Add default server (localhost).
$gmworker->addServer();

# Register function "reverse" with the server.
$gmworker->addFunction("reverse", "reverse_fn");

print "Waiting for job...\n";
while($gmworker->work())
{
  if ($gmworker->returnCode() != GEARMAN_SUCCESS)
  {
    echo "return_code: " . $gmworker->returnCode() . "\n";
    break;
  }
}

function reverse_fn($job)
{
  echo "Received job: " . $job->handle() . "\n";

  $workload = $job->workload();
  $workload_size = $job->workloadSize();

  echo "Workload: $workload ($workload_size)\n";

  # This status loop is not needed, just showing how it works
  for ($x= 0; $x < $workload_size; $x++)
  {
    echo "Sending status: " + $x + 1 . "/$workload_size complete\n";
    $job->sendStatus($x+1, $workload_size);
    $job->sendData(substr($workload, $x, 1));
    sleep(1);
  }

  $result= strrev($workload);
  echo "Result: $result\n";

  # Return what we want to send back to the client.
  return $result;
}

?>

以上例程的输出类似于:

Worker output:

Starting
Waiting for job...
Received job: H:foo.local:106
Workload: Hello! (6)
1/6 complete
2/6 complete
3/6 complete
4/6 complete
5/6 complete
6/6 complete
Result: !olleH

Client output:

Starting
Sending job
Status: 1/6 complete
Data: H
Status: 2/6 complete
Data: e
Status: 3/6 complete
Data: l
Status: 4/6 complete
Data: l
Status: 5/6 complete
Data: o
Status: 6/6 complete
Data: !
Success: !olleH

参见

  • GearmanClient::doHigh
  • GearmanClient::doLow
  • GearmanClient::doBackground
  • GearmanClient::doHighBackground
  • GearmanClient::doLowBackground

GearmanClient::doStatus

Get the status for the running task

说明

public array GearmanClient::doStatus ( <span class="methodparam">void )

Returns the status for the running task. This should be used between repeated GearmanClient::doNormal calls.

参数

此函数没有参数。

返回值

An array representing the percentage completion given as a fraction, with the first element the numerator and the second element the denomintor.

范例

示例 #1 Get the status of a long running job

The worker in this example has an artificial delay added during processing of the string to be reversed. After each delay it calls <span class="methodname">GearmanJob::status which the client then picks up.

<?php

echo "Starting\n";

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

# Add default server (localhost).
$gmclient->addServer();

echo "Sending job\n";

# Send reverse job
do
{
  $result = $gmclient->doNormal("reverse", "Hello!");

  # Check for various return packets and errors.
  switch($gmclient->returnCode())
  {
    case GEARMAN_WORK_DATA:
      break;
    case GEARMAN_WORK_STATUS:
      # get the current job status
      list($numerator, $denominator)= $gmclient->doStatus();
      echo "Status: $numerator/$denominator complete\n";
      break;
    case GEARMAN_WORK_FAIL:
      echo "Failed\n";
      exit;
    case GEARMAN_SUCCESS:
      break;
    default:
      echo "RET: " . $gmclient->returnCode() . "\n";
      exit;
  }
}
while($gmclient->returnCode() != GEARMAN_SUCCESS);

echo "Success: $result\n";

?>

以上例程的输出类似于:

Starting
Sending job
Status: 1/6 complete
Status: 2/6 complete
Status: 3/6 complete
Status: 4/6 complete
Status: 5/6 complete
Status: 6/6 complete
Success: !olleH

参见

  • GearmanClient::doNormal
  • GearmanJob::status

GearmanClient::echo

Send data to all job servers to see if they echo it back [deprecated]

说明

public bool GearmanClient::echo ( <span class="methodparam">string $workload )

The GearmanClient::echo method is deprecated as of pecl/gearman 1.0.0. Use <span class="methodname">GearmanClient::ping.

参数

workload
Some arbitrary serialized data to be echo back

返回值

成功时返回 true, 或者在失败时返回 false

GearmanClient::error

Returns an error string for the last error encountered

说明

public string GearmanClient::error ( <span class="methodparam">void )

Returns an error string for the last error encountered.

参数

此函数没有参数。

返回值

A human readable error string.

参见

  • GearmanClient::getErrno

GearmanClient::getErrno

Get an errno value

说明

public int <span class="methodname">GearmanClient::getErrno ( <span class="methodparam">void )

Value of errno in the case of a GEARMAN_ERRNO return value.

参数

此函数没有参数。

返回值

A valid Gearman errno.

参见

  • GearmanClient::error

GearmanClient::jobStatus

gearman_job_status

Get the status of a background job

说明

面向对象风格 (method):

public array GearmanClient::jobStatus ( <span class="methodparam">string $job_handle )

Gets the status for a background job given a job handle. The status information will specify whether the job is known, whether the job is currently running, and the percentage completion.

参数

job_handle
由 Gearman 服务器指派的工作句柄。

返回值

An array containing status information for the job corresponding to the supplied job handle. The first array element is a boolean indicating whether the job is even known, the second is a boolean indicating whether the job is still running, and the third and fourth elements correspond to the numerator and denominator of the fractional completion percentage, respectively.

范例

示例 #1 Monitor the status of a long running background job

<?php

/* create our object */
$gmclient= new GearmanClient();

/* add the default server */
$gmclient->addServer();

/* run reverse client */
$job_handle = $gmclient->doBackground("reverse", "this is a test");

if ($gmclient->returnCode() != GEARMAN_SUCCESS)
{
  echo "bad return code\n";
  exit;
}

$done = false;
do
{
   sleep(3);
   $stat = $gmclient->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";

?>

以上例程的输出类似于:

Running: true, numerator: 3, denomintor: 14
Running: true, numerator: 6, denomintor: 14
Running: true, numerator: 9, denomintor: 14
Running: true, numerator: 12, denomintor: 14
Running: false, numerator: 0, denomintor: 0
done!

参见

  • GearmanClient::doStatus

GearmanClient::ping

Send data to all job servers to see if they echo it back

说明

public bool GearmanClient::ping ( <span class="methodparam">string $workload )

Sends some arbitrary data to all job servers to see if they echo it back. The data sent is not used or processed in any other way. Primarily used for testing and debugging.

参数

workload
Some arbitrary serialized data to be echo back

返回值

成功时返回 true, 或者在失败时返回 false

GearmanClient::removeOptions

Remove client options

说明

public bool GearmanClient::removeOptions ( <span class="methodparam">int $options )

Removes (unsets) one or more options.

参数

options
The options to be removed (unset)

返回值

Always returns true.

GearmanClient::returnCode

Get the last Gearman return code

说明

public int <span class="methodname">GearmanClient::returnCode ( <span class="methodparam">void )

Returns the last Gearman return code.

参数

此函数没有参数。

返回值

A valid Gearman return code.

GearmanClient::runTasks

Run a list of tasks in parallel

说明

public bool GearmanClient::runTasks ( <span class="methodparam">void )

For a set of tasks previously added with <span class="methodname">GearmanClient::addTask, <span class="methodname">GearmanClient::addTaskHigh, <span class="methodname">GearmanClient::addTaskLow, <span class="methodname">GearmanClient::addTaskBackground, <span class="methodname">GearmanClient::addTaskHighBackground, or <span class="methodname">GearmanClient::addTaskLowBackground, this call starts running the tasks in parallel.

参数

此函数没有参数。

返回值

成功时返回 true, 或者在失败时返回 false

参见

  • GearmanClient::addTask

GearmanClient::setClientCallback

Callback function when there is a data packet for a task (deprecated)

说明

public void GearmanClient::setClientCallback ( <span class="methodparam">callable $callback )

Sets the callback function for accepting data packets for a task. The callback function should take a single argument, a <span class="classname">GearmanTask object.

Note:

This method has been replaced by <span class="methodname">GearmanClient::setDataCallback in the 0.6.0 release of the Gearman extension.

参数

callback
A function or method to call

返回值

成功时返回 true, 或者在失败时返回 false

参见

  • GearmanClient::setCompleteCallback
  • GearmanClient::setCreatedCallback
  • GearmanClient::setDataCallback
  • GearmanClient::setExceptionCallback
  • GearmanClient::setFailCallback
  • GearmanClient::setStatusCallback
  • GearmanClient::setWarningCallback
  • GearmanClient::setWorkloadCallback

GearmanClient::setCompleteCallback

Set a function to be called on task completion

说明

public bool GearmanClient::setCompleteCallback ( callable $callback )

Use to set a function to be called when a <span class="classname">GearmanTask is completed, or when <span class="methodname">GearmanJob::sendComplete is invoked by a worker (whichever happens first).

This callback executes only when executing a <span class="classname">GearmanTask using <span class="methodname">GearmanClient::runTasks. It is not used for individual jobs.

参数

callback
A function to be called

返回值

成功时返回 true, 或者在失败时返回 false

参见

  • GearmanClient::setDataCallback
  • GearmanClient::setCreatedCallback
  • GearmanClient::setExceptionCallback
  • GearmanClient::setFailCallback
  • GearmanClient::setStatusCallback
  • GearmanClient::setWarningCallback
  • GearmanClient::setWorkloadCallback

GearmanClient::setContext

Set application context

说明

public bool GearmanClient::setContext ( <span class="methodparam">string $context )

Sets an arbitrary string to provide application context that can later be retrieved by GearmanClient::context.

参数

context
Arbitrary context data

返回值

Always returns true.

参见

  • GearmanClient::context

GearmanClient::setCreatedCallback

Set a callback for when a task is queued

说明

public bool GearmanClient::setCreatedCallback ( string $callback )

Sets a function to be called when a task is received and queued by the Gearman job server. The callback should accept a single argument, a GearmanTask object.

参数

callback
A function to call

返回值

成功时返回 true, 或者在失败时返回 false

参见

  • GearmanClient::setDataCallback
  • GearmanClient::setCompleteCallback
  • GearmanClient::setExceptionCallback
  • GearmanClient::setFailCallback
  • GearmanClient::setStatusCallback
  • GearmanClient::setWarningCallback
  • GearmanClient::setWorkloadCallback

GearmanClient::setData

Set application data (deprecated)

说明

public bool GearmanClient::setData ( <span class="methodparam">string $data )

Sets some arbitrary application data that can later be retrieved by GearmanClient::data.

Note:

This method has been replaced by <span class="methodname">GearmanCient::setContext in the 0.6.0 release of the Gearman extension.

参数

data

返回值

Always returns true.

参见

  • GearmanClient::data

GearmanClient::setDataCallback

Callback function when there is a data packet for a task

说明

public bool GearmanClient::setDataCallback ( <span class="methodparam">callable $callback )

Sets the callback function for accepting data packets for a task. The callback function should take a single argument, a <span class="classname">GearmanTask object.

参数

callback
A function or method to call

返回值

成功时返回 true, 或者在失败时返回 false

参见

  • GearmanClient::setCompleteCallback
  • GearmanClient::setCreatedCallback
  • GearmanClient::setExceptionCallback
  • GearmanClient::setFailCallback
  • GearmanClient::setStatusCallback
  • GearmanClient::setWarningCallback
  • GearmanClient::setWorkloadCallback

GearmanClient::setExceptionCallback

Set a callback for worker exceptions

说明

public bool GearmanClient::setExceptionCallback ( callable $callback )

Specifies a function to call when a worker for a task sends an exception.

参数

callback
Function to call when the worker throws an exception

返回值

成功时返回 true, 或者在失败时返回 false

参见

  • GearmanClient::setDataCallback
  • GearmanClient::setCompleteCallback
  • GearmanClient::setCreatedCallback
  • GearmanClient::setFailCallback
  • GearmanClient::setStatusCallback
  • GearmanClient::setWarningCallback
  • GearmanClient::setWorkloadCallback

GearmanClient::setFailCallback

Set callback for job failure

说明

public bool GearmanClient::setFailCallback ( <span class="methodparam">callable $callback )

Sets the callback function to be used when a task does not complete successfully. The function should accept a single argument, a <span class="classname">GearmanTask object.

参数

callback
A function to call

返回值

成功时返回 true, 或者在失败时返回 false

参见

  • GearmanClient::setDataCallback
  • GearmanClient::setCompleteCallback
  • GearmanClient::setCreatedCallback
  • GearmanClient::setExceptionCallback
  • GearmanClient::setStatusCallback
  • GearmanClient::setWarningCallback
  • GearmanClient::setWorkloadCallback

GearmanClient::setOptions

Set client options

说明

public bool GearmanClient::setOptions ( <span class="methodparam">int $options )

Sets one or more client options.

参数

options
The options to be set

返回值

Always returns true.

GearmanClient::setStatusCallback

Set a callback for collecting task status

说明

public bool GearmanClient::setStatusCallback ( <span class="methodparam">callable $callback )

Sets a callback function used for getting updated status information from a worker. The function should accept a single argument, a <span class="classname">GearmanTask object.

参数

callback
A function to call

返回值

成功时返回 true, 或者在失败时返回 false

参见

  • GearmanClient::setDataCallback
  • GearmanClient::setCompleteCallback
  • GearmanClient::setCreatedCallback
  • GearmanClient::setExceptionCallback
  • GearmanClient::setFailCallback
  • GearmanClient::setWarningCallback
  • GearmanClient::setWorkloadCallback

GearmanClient::setTimeout

Set socket I/O activity timeout

说明

public bool GearmanClient::setTimeout ( <span class="methodparam">int $timeout )

Sets the timeout for socket I/O activity.

参数

timeout
An interval of time in milliseconds

返回值

Always returns true.

GearmanClient::setWarningCallback

Set a callback for worker warnings

说明

public bool GearmanClient::setWarningCallback ( callable $callback )

Sets a function to be called when a worker sends a warning. The callback should accept a single argument, a <span class="classname">GearmanTask object.

参数

callback
A function to call

返回值

成功时返回 true, 或者在失败时返回 false

参见

  • GearmanClient::setDataCallback
  • GearmanClient::setCompleteCallback
  • GearmanClient::setCreatedCallback
  • GearmanClient::setExceptionCallback
  • GearmanClient::setFailCallback
  • GearmanClient::setStatusCallback
  • GearmanClient::setWorkloadCallback

GearmanClient::setWorkloadCallback

Set a callback for accepting incremental data updates

说明

public bool GearmanClient::setWorkloadCallback ( callable $callback )

Sets a function to be called when a worker needs to send back data prior to job completion. A worker can do this when it needs to send updates, send partial results, or flush data during long running jobs. The callback should accept a single argument, a <span class="classname">GearmanTask object.

参数

callback
A function to call

返回值

成功时返回 true, 或者在失败时返回 false

参见

  • GearmanClient::setDataCallback
  • GearmanClient::setCompleteCallback
  • GearmanClient::setCreatedCallback
  • GearmanClient::setExceptionCallback
  • GearmanClient::setFailCallback
  • GearmanClient::setStatusCallback
  • GearmanClient::setWarningCallback

GearmanClient::timeout

Get current socket I/O activity timeout value

说明

public int <span class="methodname">GearmanClient::timeout ( <span class="methodparam">void )

Returns the timeout in milliseconds to wait for I/O activity.

参数

此函数没有参数。

返回值

Timeout in milliseconds to wait for I/O activity. A negative value means an infinite timeout.

参见

  • GearmanClient::setTimeout

简介

类摘要

GearmanJob

class GearmanJob {

/* 方法 */

public bool complete ( <span class="methodparam">string $result )

public <span class="methodname">__construct ( <span class="methodparam">void )

public bool data ( <span class="type">string $data )

public bool exception ( <span class="methodparam">string $exception )

public bool fail ( <span class="methodparam">void )

public string functionName ( <span class="methodparam">void )

public string handle ( <span class="methodparam">void )

public int <span class="methodname">returnCode ( <span class="methodparam">void )

public bool sendComplete ( <span class="methodparam">string $result )

public bool sendData ( <span class="methodparam">string $data )

public bool sendException ( <span class="methodparam">string $exception )

public bool sendFail ( <span class="methodparam">void )

public bool sendStatus ( <span class="methodparam">int $numerator , int $denominator )

public bool sendWarning ( <span class="methodparam">string $warning )

public bool setReturn ( <span class="methodparam">int $gearman_return_t )

public bool status ( <span class="type">int $numerator , <span class="methodparam">int $denominator )

public string unique ( <span class="methodparam">void )

public bool warning ( <span class="methodparam">string $warning )

public string workload ( <span class="methodparam">void )

public int <span class="methodname">workloadSize ( <span class="methodparam">void )

}

GearmanJob::complete

Send the result and complete status (deprecated)

说明

public bool GearmanJob::complete ( <span class="methodparam">string $result )

Sends result data and the complete status update for this job.

Note:

This method has been replaced by <span class="methodname">GearmanJob::sendComplete in the 0.6.0 release of the Gearman extension.

参数

result
Serialized result data.

返回值

成功时返回 true, 或者在失败时返回 false

参见

  • GearmanJob::sendFail
  • GearmanJob::setReturn

GearmanJob::__construct

Create a GearmanJob instance

说明

public <span class="methodname">GearmanJob::__construct ( <span class="methodparam">void )

Creates a GearmanJob instance representing a job the worker is to complete.

参数

此函数没有参数。

返回值

A GearmanJob object.

GearmanJob::data

Send data for a running job (deprecated)

说明

public bool GearmanJob::data ( <span class="methodparam">string $data )

Sends data to the job server (and any listening clients) for this job.

Note:

This method has been replaced by <span class="methodname">GearmanJob::sendData in the 0.6.0 release of the Gearman extension.

参数

data
Arbitrary serialized data.

返回值

成功时返回 true, 或者在失败时返回 false

参见

  • GearmanJob::workload
  • GearmanTask::data

GearmanJob::exception

Send exception for running job (deprecated)

说明

public bool GearmanJob::exception ( <span class="methodparam">string $exception )

Sends the supplied exception when this job is running.

Note:

This method has been replaced by <span class="methodname">GearmanJob::sendException in the 0.6.0 release of the Gearman extension.

参数

exception
An exception description.

返回值

成功时返回 true, 或者在失败时返回 false

参见

  • GearmanJob::setReturn
  • GearmanJob::sendStatus
  • GearmanJob::sendWarning

GearmanJob::fail

Send fail status (deprecated)

说明

public bool GearmanJob::fail ( <span class="methodparam">void )

Sends failure status for this job, indicating that the job failed in a known way (as opposed to failing due to a thrown exception).

Note:

This method has been replaced by <span class="methodname">GearmanJob::sendFail in the 0.6.0 release of the Gearman extension.

参数

此函数没有参数。

返回值

成功时返回 true, 或者在失败时返回 false

参见

  • GearmanJob::sendException
  • GearmanJob::setReturn
  • GearmanJob::sendStatus
  • GearmanJob::sendWarning

GearmanJob::functionName

Get function name

说明

public string GearmanJob::functionName ( <span class="methodparam">void )

Returns the function name for this job. This is the function the work will execute to perform the job.

参数

此函数没有参数。

返回值

The name of a function.

参见

  • GearmanTask::function

GearmanJob::handle

Get the job handle

说明

public string GearmanJob::handle ( <span class="methodparam">void )

Returns the opaque job handle assigned by the job server.

参数

此函数没有参数。

返回值

An opaque job handle.

参见

  • GearmanTask::jobHandle

GearmanJob::returnCode

Get last return code

说明

public int <span class="methodname">GearmanJob::returnCode ( <span class="methodparam">void )

Returns the last return code issued by the job server.

参数

此函数没有参数。

返回值

A valid Gearman return code.

参见

  • GearmanTask::returnCode

GearmanJob::sendComplete

Send the result and complete status

说明

public bool GearmanJob::sendComplete ( <span class="methodparam">string $result )

Sends result data and the complete status update for this job.

参数

result
Serialized result data.

返回值

成功时返回 true, 或者在失败时返回 false

参见

  • GearmanJob::sendFail
  • GearmanJob::setReturn

GearmanJob::sendData

Send data for a running job

说明

public bool GearmanJob::sendData ( <span class="methodparam">string $data )

Sends data to the job server (and any listening clients) for this job.

参数

data
Arbitrary serialized data.

返回值

成功时返回 true, 或者在失败时返回 false

参见

  • GearmanJob::workload
  • GearmanTask::data

GearmanJob::sendException

Send exception for running job (exception)

说明

public bool GearmanJob::sendException ( <span class="methodparam">string $exception )

Sends the supplied exception when this job is running.

参数

exception
An exception description.

返回值

成功时返回 true, 或者在失败时返回 false

参见

  • GearmanJob::setReturn
  • GearmanJob::sendStatus
  • GearmanJob::sendWarning

GearmanJob::sendFail

Send fail status

说明

public bool GearmanJob::sendFail ( <span class="methodparam">void )

Sends failure status for this job, indicating that the job failed in a known way (as opposed to failing due to a thrown exception).

参数

此函数没有参数。

返回值

成功时返回 true, 或者在失败时返回 false

参见

  • GearmanJob::sendException
  • GearmanJob::setReturn
  • GearmanJob::sendStatus
  • GearmanJob::sendWarning

GearmanJob::sendStatus

Send status

说明

public bool GearmanJob::sendStatus ( <span class="methodparam">int $numerator , int $denominator )

Sends status information to the job server and any listening clients. Use this to specify what percentage of the job has been completed.

参数

numerator
The numerator of the precentage completed expressed as a fraction.

denominator
The denominator of the precentage completed expressed as a fraction.

返回值

成功时返回 true, 或者在失败时返回 false

参见

  • GearmanClient::jobStatus
  • GearmanTask::taskDenominator
  • GearmanTask::taskNumerator

GearmanJob::sendWarning

Send a warning

说明

public bool GearmanJob::sendWarning ( <span class="methodparam">string $warning )

Sends a warning for this job while it is running.

参数

warning
A warning message.

返回值

成功时返回 true, 或者在失败时返回 false

参见

  • GearmanJob::sendComplete
  • GearmanJob::sendException
  • GearmanJob::sendFail

GearmanJob::setReturn

Set a return value

说明

public bool GearmanJob::setReturn ( <span class="methodparam">int $gearman_return_t )

Sets the return value for this job, indicates how the job completed.

参数

gearman_return_t
A valid Gearman return value.

返回值

Description...

GearmanJob::status

Send status (deprecated)

说明

public bool GearmanJob::status ( <span class="methodparam">int $numerator , int $denominator )

Sends status information to the job server and any listening clients. Use this to specify what percentage of the job has been completed.

Note:

This method has been replaced by <span class="methodname">GearmanJob::sendStatus in the 0.6.0 release of the Gearman extenstion.

参数

numerator
The numerator of the precentage completed expressed as a fraction.

denominator
The denominator of the precentage completed expressed as a fraction.

返回值

成功时返回 true, 或者在失败时返回 false

参见

  • GearmanClient::jobStatus
  • GearmanTask::taskDenominator
  • GearmanTask::taskNumerator

GearmanJob::unique

Get the unique identifier

说明

public string GearmanJob::unique ( <span class="methodparam">void )

Returns the unique identifiter for this job. The identifier is assigned by the client.

参数

此函数没有参数。

返回值

An opaque unique identifier.

参见

  • GearmanClient::do
  • GearmanTask::uuid

GearmanJob::warning

Send a warning (deprecated)

说明

public bool GearmanJob::warning ( <span class="methodparam">string $warning )

Sends a warning for this job while it is running.

Note:

This method has been replaced by <span class="methodname">GearmanJob::sendWarning in the 0.6.0 release of the Gearman extension.

参数

warning
A warning messages.

返回值

成功时返回 true, 或者在失败时返回 false

参见

  • GearmanJob::sendComplete
  • GearmanJob::sendException
  • GearmanJob::sendFail

GearmanJob::workload

Get workload

说明

public string GearmanJob::workload ( <span class="methodparam">void )

Returns the workload for the job. This is serialized data that is to be processed by the worker.

参数

此函数没有参数。

返回值

Serialized data.

参见

  • GearmanClient::do
  • GearmanJob::workloadSize

GearmanJob::workloadSize

Get size of work load

说明

public int <span class="methodname">GearmanJob::workloadSize ( <span class="methodparam">void )

Returns the size of the job's work load (the data the worker is to process) in bytes.

参数

此函数没有参数。

返回值

The size in bytes.

参见

  • GearmanJob::workload

简介

类摘要

GearmanTask

class GearmanTask {

/* 方法 */

public <span class="methodname">__construct ( <span class="methodparam">void )

public <span class="type">GearmanTaskfalse create ( <span class="methodparam">void )

public string data ( <span class="methodparam">void )

public int <span class="methodname">dataSize ( <span class="methodparam">void )

public string function ( <span class="methodparam">void )

public string functionName ( <span class="methodparam">void )

public bool isKnown ( <span class="methodparam">void )

public bool isRunning ( <span class="methodparam">void )

public string jobHandle ( <span class="methodparam">void )

public array recvData ( <span class="methodparam">int $data_len )

public int <span class="methodname">returnCode ( <span class="methodparam">void )

public int <span class="methodname">sendData ( <span class="type">string $data )

public int <span class="methodname">sendWorkload ( <span class="type">string $data )

public int <span class="methodname">taskDenominator ( <span class="methodparam">void )

public int <span class="methodname">taskNumerator ( <span class="methodparam">void )

public string unique ( <span class="methodparam">void )

public string uuid ( <span class="methodparam">void )

}

GearmanTask::__construct

Create a GearmanTask instance

说明

public <span class="methodname">GearmanTask::__construct ( <span class="methodparam">void )

Creates a GearmanTask instance representing a task to be submitted to a job server.

参数

此函数没有参数。

返回值

A GearmanTask object.

GearmanTask::create

Create a task (deprecated)

说明

public <span class="type">GearmanTaskfalse GearmanTask::create ( <span class="methodparam">void )

Returns a new GearmanTask object.

Note:

This method was removed in the 0.6.0 version of the Gearman extension.

参数

此函数没有参数。

返回值

A GearmanTask oject 或者在失败时返回 false.

GearmanTask::data

Get data returned for a task

说明

public string GearmanTask::data ( <span class="methodparam">void )

Returns data being returned for a task by a worker.

参数

此函数没有参数。

返回值

The serialized data, or false if no data is present.

参见

  • GearmanTask::dataSize

GearmanTask::dataSize

Get the size of returned data

说明

public int <span class="methodname">GearmanTask::dataSize ( <span class="methodparam">void )

Returns the size of the data being returned for a task.

参数

此函数没有参数。

返回值

The data size, or false if there is no data.

参见

  • GearmanTask::data

GearmanTask::function

Get associated function name (deprecated)

说明

public string GearmanTask::function ( <span class="methodparam">void )

Returns the name of the function this task is associated with, i.e., the function the Gearman worker calls.

Note:

This method has been replaced by <span class="methodname">GearmanTask::functionName in the 0.6.0 release of the Gearman extension.

参数

此函数没有参数。

返回值

A function name.

GearmanTask::functionName

Get associated function name

说明

public string GearmanTask::functionName ( <span class="methodparam">void )

Returns the name of the function this task is associated with, i.e., the function the Gearman worker calls.

参数

此函数没有参数。

返回值

A function name.

GearmanTask::isKnown

Determine if task is known

说明

public bool GearmanTask::isKnown ( <span class="methodparam">void )

Gets the status information for whether or not this task is known to the job server.

参数

此函数没有参数。

返回值

true if the task is known, false otherwise.

GearmanTask::isRunning

Test whether the task is currently running

说明

public bool GearmanTask::isRunning ( <span class="methodparam">void )

Indicates whether or not this task is currently running.

参数

此函数没有参数。

返回值

true if the task is running, false otherwise.

GearmanTask::jobHandle

gearman_job_handle

Get the job handle

说明

public string GearmanTask::jobHandle ( <span class="methodparam">void )

Returns the job handle for this task.

参数

此函数没有参数。

返回值

The opaque job handle.

参见

  • GearmanClient::doJobHandle

GearmanTask::recvData

Read work or result data into a buffer for a task

说明

public array GearmanTask::recvData ( <span class="methodparam">int $data_len )

Warning

此函数是实验性的。此函数的表象,包括名称及其相关文档都可能在未来的 PHP 发布版本中未通知就被修改。使用本函数风险自担。

参数

data_len
Length of data to be read.

返回值

An array whose first element is the length of data read and the second is the data buffer. Returns false if the read failed.

参见

  • GearmanTask::sendData

GearmanTask::returnCode

Get the last return code

说明

public int <span class="methodname">GearmanTask::returnCode ( <span class="methodparam">void )

Returns the last Gearman return code for this task.

参数

此函数没有参数。

返回值

A valid Gearman return code.

参见

  • GearmanClient::returnCode

GearmanTask::sendData

Send data for a task (deprecated)

说明

public int <span class="methodname">GearmanTask::sendData ( <span class="methodparam">string $data )

Warning

此函数是实验性的。此函数的表象,包括名称及其相关文档都可能在未来的 PHP 发布版本中未通知就被修改。使用本函数风险自担。

参数

data
Data to send to the worker.

返回值

The length of data sent, or false if the send failed.

参见

  • GearmanTask::recvData

GearmanTask::sendWorkload

Send data for a task

说明

public int <span class="methodname">GearmanTask::sendWorkload ( <span class="methodparam">string $data )

Warning

此函数是实验性的。此函数的表象,包括名称及其相关文档都可能在未来的 PHP 发布版本中未通知就被修改。使用本函数风险自担。

参数

data
Data to send to the worker.

返回值

The length of data sent, or false if the send failed.

参见

  • GearmanTask::recvData

GearmanTask::taskDenominator

Get completion percentage denominator

说明

public int <span class="methodname">GearmanTask::taskDenominator ( <span class="methodparam">void )

Returns the denominator of the percentage of the task that is complete expressed as a fraction.

参数

此函数没有参数。

返回值

A number between 0 and 100, or false if cannot be determined.

参见

  • GearmanTask::taskNumerator

GearmanTask::taskNumerator

Get completion percentage numerator

说明

public int <span class="methodname">GearmanTask::taskNumerator ( <span class="methodparam">void )

Returns the numerator of the percentage of the task that is complete expressed as a fraction.

参数

此函数没有参数。

返回值

A number between 0 and 100, or false if cannot be determined.

参见

  • GearmanTask::taskDenominator

GearmanTask::unique

Get the unique identifier for a task

说明

public string GearmanTask::unique ( <span class="methodparam">void )

Returns the unique identifier for this task. This is assigned by the GearmanClient, as opposed to the job handle which is set by the Gearman job server.

参数

此函数没有参数。

返回值

The unique identifier, or false if no identifier is assigned.

参见

  • GearmanClient::do
  • GearmanClient::addTask

GearmanTask::uuid

Get the unique identifier for a task (deprecated)

说明

public string GearmanTask::uuid ( <span class="methodparam">void )

Returns the unique identifier for this task. This is assigned by the GearmanClient, as opposed to the job handle which is set by the Gearman job server.

Note:

This method has been replaced by <span class="methodname">GearmanTask::unique in the 0.6.0 release of the Gearman extension.

参数

此函数没有参数。

返回值

The unique identifier, or false if no identifier is assigned.

参见

  • GearmanClient::do
  • GearmanClient::addTask

简介

类摘要

GearmanWorker

class GearmanWorker {

/* 方法 */

public bool addFunction ( <span class="methodparam">string $function_name , <span class="type">callable $function [, <span class="methodparam">mixed &$context [, int $timeout ]] )

public bool addOptions ( <span class="methodparam">int $option )

public bool addServer ([ <span class="methodparam">string $host<span class="initializer"> = 127.0.0.1 [, <span class="methodparam">int $port<span class="initializer"> = 4730 ]] )

public bool addServers ( <span class="methodparam">string $servers<span class="initializer"> = 127.0.0.1:4730 )

public void clone ( <span class="methodparam">void )

public <span class="methodname">__construct ( <span class="methodparam">void )

public bool echo ( <span class="type">string $workload )

public string error ( <span class="methodparam">void )

public int <span class="methodname">getErrno ( <span class="methodparam">void )

public int <span class="methodname">options ( <span class="methodparam">void )

public bool register ( <span class="methodparam">string $function_name [, <span class="type">int $timeout ] )

public bool removeOptions ( <span class="methodparam">int $option )

public int <span class="methodname">returnCode ( <span class="methodparam">void )

public bool setId ( <span class="type">string $id )

public bool setOptions ( <span class="methodparam">int $option )

public bool setTimeout ( <span class="methodparam">int $timeout )

public int <span class="methodname">timeout ( <span class="methodparam">void )

public bool unregister ( <span class="methodparam">string $function_name )

public bool unregisterAll ( <span class="methodparam">void )

public bool wait ( <span class="methodparam">void )

public bool work ( <span class="methodparam">void )

}

GearmanWorker::addFunction

Register and add callback function

说明

public bool GearmanWorker::addFunction ( <span class="methodparam">string $function_name , <span class="type">callable $function [, <span class="methodparam">mixed &$context [, int $timeout ]] )

Registers a function name with the job server and specifies a callback corresponding to that function. Optionally specify extra application context data to be used when the callback is called and a timeout.

参数

function_name
The name of a function to register with the job server

function
A callback that gets called when a job for the registered function name is submitted

context
A reference to arbitrary application context data that can be modified by the worker function

timeout
An interval of time in seconds

返回值

成功时返回 true, 或者在失败时返回 false

范例

示例 #1 Simple worker making use of extra application context data

<?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()); 
} 

?>

Running a client that submits two jobs for the reverse function would have output similar to the following:

1: olleh
2: dlrow

参见

  • GearmanClient::do

GearmanWorker::addOptions

Add worker options

说明

public bool GearmanWorker::addOptions ( <span class="methodparam">int $option )

Adds one or more options to the options previously set.

参数

option
The options to be added

返回值

Always returns true.

参见

  • GearmanWorker::options
  • GearmanClient::setOptions
  • GearmanClient::removeOptions

GearmanWorker::addServer

Add a job server

说明

public bool GearmanWorker::addServer ([ <span class="methodparam">string $host<span class="initializer"> = 127.0.0.1 [, <span class="methodparam">int $port<span class="initializer"> = 4730 ]] )

Adds a job server to this worker. This goes into a list of servers than can be used to run jobs. No socket I/O happens here.

参数

host
任务服务器主机名。

port
任务服务器端口号。

返回值

成功时返回 true, 或者在失败时返回 false

范例

示例 #1 Add alternate Gearman servers

<?php
$worker= new GearmanWorker(); 
$worker->addServer("10.0.0.1"); 
$worker->addServer("10.0.0.2", 7003);
?>

参见

  • GearmanWorker::addServers

GearmanWorker::addServers

Add job servers

说明

public bool GearmanWorker::addServers ( <span class="methodparam">string $servers<span class="initializer"> = 127.0.0.1:4730 )

Adds one or more job servers to this worker. These go into a list of servers that can be used to run jobs. No socket I/O happens here.

参数

servers
A comma separated list of job servers in the format host:port. If no port is specified, it defaults to 4730.

返回值

成功时返回 true, 或者在失败时返回 false

范例

示例 #1 Add two job servers

<?php

$worker= new GearmanWorker(); 
$worker->addServers("10.0.0.1,10.0.0.2:7003");

?>

参见

  • GearmanWorker::addServer

GearmanWorker::clone

Create a copy of the worker

说明

public void GearmanWorker::clone ( <span class="methodparam">void )

Creates a copy of the worker.

参数

此函数没有参数。

返回值

A GearmanWorker object

GearmanWorker::__construct

Create a GearmanWorker instance

说明

public <span class="methodname">GearmanWorker::__construct ( <span class="methodparam">void )

Creates a GearmanWorker instance representing a worker that connects to the job server and accepts tasks to run.

参数

此函数没有参数。

返回值

A GearmanWorker object

参见

  • GearmanWorker::clone

GearmanWorker::echo

Test job server response

说明

public bool GearmanWorker::echo ( <span class="methodparam">string $workload )

Sends data to all job servers to see if they echo it back. This is a test function to see if job servers are responding properly.

参数

workload
Arbitrary serialized data

返回值

Standard Gearman return value.

参见

  • GearmanClient::echo

GearmanWorker::error

Get the last error encountered

说明

public string GearmanWorker::error ( <span class="methodparam">void )

Returns an error string for the last error encountered.

参数

此函数没有参数。

返回值

An error string.

参见

  • GearmanWorker::getErrno

GearmanWorker::getErrno

Get errno

说明

public int <span class="methodname">GearmanWorker::getErrno ( <span class="methodparam">void )

Returns the value of errno in the case of a GEARMAN_ERRNO return value.

参数

此函数没有参数。

返回值

A valid errno.

参见

  • GearmanWorker::error

GearmanWorker::options

Get worker options

说明

public int <span class="methodname">GearmanWorker::options ( <span class="methodparam">void )

Gets the options previously set for the worker.

参数

此函数没有参数。

返回值

The options currently set for the worker.

参见

  • GearmanWorker::setOptions

GearmanWorker::register

Register a function with the job server

说明

public bool GearmanWorker::register ( <span class="methodparam">string $function_name [, <span class="type">int $timeout ] )

Registers a function name with the job server with an optional timeout. The timeout specifies how many seconds the server will wait before marking a job as failed. If the timeout is set to zero, there is no timeout.

参数

function_name
The name of a function to register with the job server

timeout
An interval of time in seconds

返回值

A standard Gearman return value.

参见

  • GearmanWorker::unregister
  • GearmanWorker::unregisterAll

GearmanWorker::removeOptions

Remove worker options

说明

public bool GearmanWorker::removeOptions ( <span class="methodparam">int $option )

Removes (unsets) one or more worker options.

参数

option
The options to be removed (unset)

返回值

Always returns true.

参见

  • GearmanWorker::options
  • GearmanWorker::setOptions
  • GearmanWorker::addOptions

GearmanWorker::returnCode

Get last Gearman return code

说明

public int <span class="methodname">GearmanWorker::returnCode ( <span class="methodparam">void )

Returns the last Gearman return code.

参数

此函数没有参数。

返回值

A valid Gearman return code.

参见

  • GearmanWorker::error
  • GearmanWorker::getErrno

GearmanWorker::setId

Give the worker an identifier so it can be tracked when asking gearmand for the list of available workers

说明

public bool GearmanWorker::setId ( <span class="methodparam">string $id )

Assigns the worker an identifier.

参数

id
A string identifier.

返回值

成功时返回 true, 或者在失败时返回 false

范例

示例 #1 GearmanWorker::setId example

Set an identifier for a simple worker.

<?php
$worker= new GearmanWorker();
$worker->setId('test');
?>

以上例程的输出类似于:

Run the following command:
gearadmin --workers

Output:
30 ::3a3a:3361:3361:3a33%976303667 - : test

GearmanWorker::setOptions

Set worker options

说明

public bool GearmanWorker::setOptions ( <span class="methodparam">int $option )

Sets one or more options to the supplied value.

参数

option
The options to be set

返回值

Always returns true.

参见

  • GearmanWorker::options
  • GearmanWorker::addOptions
  • GearmanWorker::removeOptions
  • GearmanClient::setOptions

GearmanWorker::setTimeout

Set socket I/O activity timeout

说明

public bool GearmanWorker::setTimeout ( <span class="methodparam">int $timeout )

Sets the interval of time to wait for socket I/O activity.

参数

timeout
An interval of time in milliseconds. A negative value indicates an infinite timeout.

返回值

Always returns true.

范例

示例 #1 A simple worker with a 5 second timeout

<?php

echo "Starting\n";

# Create our worker object.
$gmworker= new GearmanWorker();

# Add default server (localhost).
$gmworker->addServer();

# Register function "reverse" with the server.
$gmworker->addFunction("reverse", "reverse_fn");

# Set the timeout to 5 seconds
$gmworker->setTimeout(5000);

echo "Waiting for job...\n";
while(@$gmworker->work() || $gmworker->returnCode() == GEARMAN_TIMEOUT)
{
  if ($gmworker->returnCode() == GEARMAN_TIMEOUT)
  {
    # Normally one would want to do something useful here ...
    echo "Timeout. Waiting for next job...\n";
    continue;
  }

  if ($gmworker->returnCode() != GEARMAN_SUCCESS)
  {
    echo "return_code: " . $gmworker->returnCode() . "\n";
    break;
  }
}

echo "Done\n";

function reverse_fn($job)
{
  return strrev($job->workload());
}

?>

Running the worker with no submitted jobs will generate output that looks like the following:

Starting
Waiting for job...
Timeout. Waiting for next job...
Timeout. Waiting for next job...
Timeout. Waiting for next job...

参见

  • GearmanWorker::timeout

GearmanWorker::timeout

Get socket I/O activity timeout

说明

public int <span class="methodname">GearmanWorker::timeout ( <span class="methodparam">void )

Returns the current time to wait, in milliseconds, for socket I/O activity.

参数

此函数没有参数。

返回值

A time period is milliseconds. A negative value indicates an infinite timeout.

参见

  • GearmanWorker::setTimeout

GearmanWorker::unregister

Unregister a function name with the job servers

说明

public bool GearmanWorker::unregister ( <span class="methodparam">string $function_name )

Unregisters a function name with the job servers ensuring that no more jobs (for that function) are sent to this worker.

参数

function_name
The name of a function to register with the job server

返回值

A standard Gearman return value.

参见

  • GearmanWorker::register
  • GearmanWorker::unregisterAll

GearmanWorker::unregisterAll

Unregister all function names with the job servers

说明

public bool GearmanWorker::unregisterAll ( <span class="methodparam">void )

Unregisters all previously registered functions, ensuring that no more jobs are sent to this worker.

参数

此函数没有参数。

返回值

A standard Gearman return value.

参见

  • GearmanWorker::register
  • GearmanWorker::unregister

GearmanWorker::wait

Wait for activity from one of the job servers

说明

public bool GearmanWorker::wait ( <span class="methodparam">void )

Causes the worker to wait for activity from one of the Gearman job servers when operating in non-blocking I/O mode. On failure, issues a E_WARNING with the last Gearman error encountered.

参数

此函数没有参数。

返回值

成功时返回 true, 或者在失败时返回 false

范例

示例 #1 Running worker in non-blocking mode

<?php

echo "Starting\n";

# Create our worker object
$worker= new GearmanWorker();

# Make the worker non-blocking
$worker->addOptions(GEARMAN_WORKER_NON_BLOCKING); 

# Add the default server (localhost, port 4730)
$worker->addServer(); 

# Add our reverse function
$worker->addFunction('reverse', 'reverse_fn');

# Try to grab a job
while (@$worker->work() ||
       $worker->returnCode() == GEARMAN_IO_WAIT ||
       $worker->returnCode() == GEARMAN_NO_JOBS)
{
  if ($worker->returnCode() == GEARMAN_SUCCESS)
    continue;

  echo "Waiting for next job...\n";
  if (!@$worker->wait()) 
  { 
    if ($worker->returnCode() == GEARMAN_NO_ACTIVE_FDS) 
    { 
      # We are not connected to any servers, so wait a bit before 
      # trying to reconnect. 
      sleep(5); 
      continue; 
    } 
    break; 
  } 
} 

echo "Worker Error: " . $worker->error() . "\n";

function reverse_fn($job)
{
  return strrev($job->workload());
}


?>

参见

  • GearmanWorker::work

GearmanWorker::work

Wait for and perform jobs

说明

public bool GearmanWorker::work ( <span class="methodparam">void )

Waits for a job to be assigned and then calls the appropriate callback function. Issues an E_WARNING with the last Gearman error if the return code is not one of GEARMAN_SUCCESS, GEARMAN_IO_WAIT, or GEARMAN_WORK_FAIL.

参数

此函数没有参数。

返回值

成功时返回 true, 或者在失败时返回 false

范例

示例 #1 GearmanWorker::work example

<?php

# create the worker
$worker = new GearmanWorker(); 

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

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

# start te worker listening for job submissions
while ($worker->work()); 

function my_reverse_function($job) 
{ 
  return strrev($job->workload()); 
}

?>

参见

  • GearmanWorker::addFunction

简介

类摘要

GearmanException

class GearmanException <span class="ooclass"> extends Exception {

/* 继承的属性 */

protected string $message ;

protected int $code ;

protected string $file ;

protected int $line ;

/* 方法 */

/* 继承的方法 */

final public string <span class="methodname">Exception::getMessage ( <span class="methodparam">void )

final public Throwable <span class="methodname">Exception::getPrevious ( <span class="methodparam">void )

final public mixed <span class="methodname">Exception::getCode ( <span class="methodparam">void )

final public string <span class="methodname">Exception::getFile ( <span class="methodparam">void )

final public int <span class="methodname">Exception::getLine ( <span class="methodparam">void )

final public array <span class="methodname">Exception::getTrace ( <span class="methodparam">void )

final public string <span class="methodname">Exception::getTraceAsString ( <span class="methodparam">void )

public string Exception::__toString ( <span class="methodparam">void )

final <span class="modifier">private void <span class="methodname">Exception::__clone ( <span class="methodparam">void )

}


本站为非盈利网站,作品由网友提供上传,如无意中有侵犯您的版权,请联系删除