Gearman PHP 2000任务时间更短

时间:2013-08-27 11:17:51

标签: php curl gearman

我在我的VPS服务器上安装了Gearman,在阅读了Gearman的文档后,我创建了两个脚本来并行处理2000个任务。

此工作人员脚本

<?php
$worker= new GearmanWorker();
$worker->addServer('localhost');
$worker->addFunction("fetchUrl", "my_reverse_function");
while ($worker->work());

function my_reverse_function($job)
{

 $curl_connection = curl_init($job->workload());
 curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
 curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);


 $data = json_decode(curl_exec($curl_connection), true);
 curl_close($curl_connection);
  return $data;
}
?>

这是客户端脚本,有2,000个任务

<?php

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

# 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("fetchUrl", "http://**.com/getaccount.php?username=** ", &$results, "t1");
$client->addTask("fetchUrl", "http://**.com/getaccount.php?username=** ", &$results, "t2");
$client->addTask("fetchUrl", "http://**.com/getaccount.php?username=** ", &$results, "t3");
$client->addTask("fetchUrl", "http://**.com/getaccount.php?username=** ", &$results, "t4");
$client->addTask("fetchUrl", "http://**.com/getaccount.php?username=** ", &$results, "t5");
$client->addTask("fetchUrl", "http://**.com/getaccount.php?username=** ", &$results, "t6");
$client->addTask("fetchUrl", "http://**.com/getaccount.php?username=** ", &$results, "t7");

.... 
....

// till t2000 taks

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

?>

我也安装了Gearman监视器脚本,一切都还可以,但问题是如何在更短的时间内处理和运行所有任务,因为这需要大约15分钟。

此致

0 个答案:

没有答案