0

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

这个 Worker 脚本

<?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 分钟。

问候

4

0 回答 0