Examples: Multi-Query

In this example we know that we need to fetch several result sets from a database. Traditionally you would make the requests one after the other, gathering results, and finally outputting a page. We’re going to use gearman to execute these queries in parallel to speed up the entire operation.

The Client

The client here could be your webapp, but for the purpose of being quick to demonstrate and try, we’ll stick a command line script.

phpjavaperl

<?php

$client = new GearmanClient();
$client->addServer(); // initialize the results of our 3 "query results" here
$userInfo = $friends = $posts = null; // 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);

The Worker

The worker, on the other hand, is still pretty simple: emulate the delay you might see when doing some real work and return a dummy result.

phpjavaperl

<?php

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

The Payoff

“Hey, I ran your stupid code and it took 9 seconds! It’s not at all faster!”

# ./run/client/here
Fetching...
Got user info in: 9.00 seconds:
string(59) "The user requested (joe@joe.com) is 7 feet tall and awesome"
string(56) "The user (joe@joe.com) is 1 degree away from Kevin Bacon"
string(43) "The user (joe@joe.com) has no posts, sorry!"

Ouch, yeah, There’s a comment in the code snippet that states:

Here we queue up multiple tasks to be execute in as much parallelism as gearmand can give us

What this means is that gearman will only run tasks in parallel if there are enough workers to accomplish that. Failing that, it will run them with as much concurrency as it can muster up from available workers. So, if you spin up three (or more) workers you’ll see this:

# ./run/client/here
Fetching...
Got user info in: 3.00 seconds:
string(59) "The user requested (joe@joe.com) is 7 feet tall and awesome"
string(56) "The user (joe@joe.com) is 1 degree away from Kevin Bacon"
string(43) "The user (joe@joe.com) has no posts, sorry!"

And now our code is significantly faster – in fact, it’s only as slow as the slowest query. Parallelism can dramatically speed up pages where tasks must be done while the user waits for a response but that, done in serial, can take a long time.

如果开启多个worker就可以并发的执行多个任务了

最新文章

  1. 编程轶事-java中的null-遁地龙卷风
  2. 55. 2种方法求字符串的组合[string combination]
  3. Apache JMeter安装
  4. 利用Abot爬虫和visjs 呈现漫威宇宙
  5. Sqlserver2008和Oracle分页语句
  6. Deep Learning(深度学习)学习笔记整理(二)
  7. Android学习整理之Activity生命周期篇
  8. Javascript基础系列之(五)关键字和保留字 (keyword)
  9. wdcp升级php版本到5.3,5.5
  10. 谈谈php中上传文件的处理
  11. 1:scrapy框架原理与环境搭设
  12. day9_python学习笔记_chapter12_模块
  13. 基于DDD的.NET项目搭建
  14. javascript中事件对象注册与删除
  15. axios 发送post请求的时候会发送两次
  16. WebSocket.之.基础入门-前端发送消息
  17. go 内嵌对象类型
  18. FREETEXTBOX
  19. 直接插入排序(高级版)之C++实现
  20. 微信小程序 - 更改radio和checkbox选中样式

热门文章

  1. git常用别名设置,保存一份
  2. BEC translation exercise 7
  3. cocos2d-html5 中的性能优化
  4. 获得客户端ip
  5. Mybatis中对于标签的配置可能不会出现自动提示解决方案
  6. shell split函数的使用
  7. 学习动态性能表(18)--v$system_event
  8. Oracle中OEM的启动与关闭
  9. 使用bat文件实现批量重命名功能
  10. Python 算法之二分查找