= 0.5.0)GearmanClient::doBackground — Run a task in the background說明public GearmanClient::doBackground(strin">

GearmanClient::doBackground

(PECL gearman >= 0.5.0)

GearmanClient::doBackgroundRun a task in the background

說明

public GearmanClient::doBackground(string $function_name, string $workload, string $unique = ?): string

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

參數(shù)

function_name

由程序自動執(zhí)行的已注冊函數(shù)。

workload

被處理的序列化數(shù)據(jù)。

unique

用于標識特定任務(wù)的唯一性 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!

參見