array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'ja', ), 'this' => array ( 0 => 'gearmanclient.dobackground.php', 1 => 'GearmanClient::doBackground', ), 'up' => array ( 0 => 'class.gearmanclient.php', 1 => 'GearmanClient', ), 'prev' => array ( 0 => 'gearmanclient.do.php', 1 => 'GearmanClient::do', ), 'next' => array ( 0 => 'gearmanclient.dohigh.php', 1 => 'GearmanClient::doHigh', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'reference/gearman/gearmanclient/dobackground.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(PECL gearman >= 0.5.0)
GearmanClient::doBackground — Run a task in the background
$function
, string $workload
, ?string $unique
= null
): stringRuns a task in the background, returning a job handle which can be used to get the status of the running task.
function
ワーカーが実行するために登録した関数。
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!