Php gearman和多重卷曲请求
如何与gearman并行运行多个curl请求我对文档有点困惑: 我的问题是:Php gearman和多重卷曲请求,php,curl,gearman,Php,Curl,Gearman,如何与gearman并行运行多个curl请求我对文档有点困惑: 我的问题是: 1。在哪里运行RunCurl();在客户机或工作机中运行? 2。是否需要将$job引用传递给RunCurl();功能? 3。哪里定义了addFunction('lookup\u user')? public function RunCurl() { //do curl request //get list of IP's from database //run Curl request } 客户 工人 在哪里运
1。在哪里运行RunCurl();在客户机或工作机中运行?
2。是否需要将$job引用传递给RunCurl();功能?
3。哪里定义了
addFunction('lookup\u user')
?
public function RunCurl()
{
//do curl request
//get list of IP's from database
//run Curl request
}
客户
工人
RunCurl()
调用在您的Web服务器(即客户端)之外进行处理,那么您需要将它放在worker中
sendException
),则实际上只需要传入作业引用。您可能至少需要向您的员工提供某种输入,尽管这不是必需的
GearmanWorker
对象上定义了addFunction
方法。请参阅该类的文档,或参阅该函数的C源代码
最后但并非最不重要的是,您的总体问题是:如何与gearman并行运行多个curl请求
gearman背后的想法是将处理工作转移到其他旨在处理特定作业的工作上。在与gearman打交道时,基本上有三种类型的呼叫:“解雇并忘记”是指你把工作交给员工,然后忘记它(我认为gearman最擅长的地方),“阻止”是指客户实际坐着等待员工返回一些数据的服务呼叫,以及“向我发送状态更新”发送作业,然后只将状态更新从工作者发送回客户端
您可以通过启动一整组工作进程,然后向这些工作进程提交一组作业来处理并行执行多个服务调用。gearmand本身只不过是一种排队服务,它跟踪哪些工人已注册/可用,并在他们进入可用工人时分配工作。一旦工人有了工作,在该工作完成之前,该工作将不再可用。通过运行多个辅助进程,虽然理论上您可以并行处理有限数量的任务(其中您可以并行处理的任务数量等于您启动的辅助进程数量)。因此,为了让我并行运行5个curl请求,我需要设置5个辅助进程?因此,在命令行中,它将类似于
$app index.php/controller\u name parallel\u worker
5次,然后运行parallel\u client
?正确。在终端中,我可能会在我的工作人员周围包装一个应用程序,因此在5个不同的终端窗口中,它更像$php my_worker_script.php
。您仍然可以使用2个工作人员处理5个请求,但在任何给定时间只处理2个请求。
public function parallel_client()
{
$client = new GearmanClient();
$client->addServer();
// initialize the results of our 3 "query results" here
$userInfo ='single';
$friends ='none';
$posts = 'jones';
// This sets up what gearman will callback to as tasks are returned to us.
// The $context helps us know which function is being returned so we can
// handle it correctly.
$client->setCompleteCallback(function(GearmanTask $task, $context) use (&$userInfo, &$friends, &$posts) {
switch($context) {
case 'lookup_user':
$userInfo = $task->data();
break;
case 'baconate':
$friends = $task->data();
break;
case 'get_latest_posts_by':
$posts = $task->data();
break;
}
});
// Here we queue up multiple tasks to be execute in *as much* parallelism as gearmand can give us
$client->addTask('lookup_user', 'joe@joe.com', 'lookup_user');
$client->addTask('baconate', 'joe@joe.com', 'baconate');
$client->addTask('get_latest_posts_by', 'joe@joe.com', 'get_latest_posts_by');
echo "Fetching...\n";
$start = microtime(true);
$client->runTasks();
$totaltime = number_format(microtime(true) - $start, 2);
echo "Got user info in: $totaltime seconds:\n";
var_dump($userInfo, $friends, $posts);
}
public function parallel_worker()
{
$worker = new GearmanWorker();
$worker->addServer();
$worker->addFunction('lookup_user', function(GearmanJob $job){
// normally you'd so some very safe type checking and query binding to a database here.
// ...and we're gonna fake that.
//sleep(3);
//$this->RunCurl();
return 'The user requested ('. $job->workload() .') is 7 feet tall and awesome!';
});
$worker->addFunction('baconate', function(GearmanJob $job){
// sleep(3);
return 'The user ('. $job->workload() .') is 1 degree away from Kevin Bacon';
});
$worker->addFunction('get_latest_posts_by', function(GearmanJob $job){
// sleep(3);
return 'The user ('. $job->workload() .') has no posts, sorry!';
});
while ($worker->work());
}