参考:
1.小喵爱你的博客
2.PHP Manual
依赖
1.gcc44
2.boost >=1.39
3.libevent
4.php5.3+
5.update ld.so.conf
安装依赖(Ubuntu 14.04 LTS)
| $ sudo apt-get install libboost-dev |
| $ sudo apt-get install libevent-dev |
| $ sudo apt-get install libgearman-dev |
| $ sudo apt-get install gearman-job-server |
安装gearman到PHP
| $ wget http://pecl.php.net/get/gearman-1.1.2.tgz |
| $ tar xvzf gearman-1.1.2.tgz |
| $ cd gearman-1.1.2/ |
| $ /usr/local/php/bin/phpize |
| $ ./configure --with-php-config=/usr/local/php/bin/php-config |
| $ make |
| $ sudo make install |
| Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/ |
编辑php.ini,添加:
| extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/gearman.so |
重启apache,检查验证
| $ sduo service httpd restart |
| $ /usr/local/php/bin/php --info | grep gearman |
| gearman |
| gearman support => enabled |
| libgearman version => 1.0.6 |
| PWD => /home/www/Downloads/gearman-1.1.2 |
| _SERVER["PWD"] => /home/www/Downloads/gearman-1.1.2 |
启动gearman服务
| $ sudo gearmand -d -u daemon --log-file=/tmp/gearman.log |
运行DEMO程序
GearmanClient.php
| <?php |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| $gmc= new GearmanClient(); |
| |
| |
| |
| |
| |
| $gmc->addServer(); |
| |
| |
| |
| |
| |
| $gmc->setCompleteCallback("reverse_complete"); |
| |
| $gmc->setStatusCallback("reverse_status"); |
| |
| |
| |
| |
| |
| $task= $gmc->addTask("reverse", "Hello World!", null, "1"); |
| |
| |
| |
| |
| |
| $task= $gmc->addTaskBackground("reverse", "!dlroW olleH", null, "2"); |
| |
| |
| |
| if (! $gmc->runTasks()) |
| |
| { |
| |
| echo "ERROR " . $gmc->error() . "\n"; |
| |
| exit; |
| |
| } |
| |
| |
| |
| echo "DONE\n"; |
| |
| |
| |
| function reverse_status($task) |
| |
| { |
| |
| echo "STATUS: " . $task->unique() . ", " . $task->jobHandle() . " - " . $task->taskNumerator() . |
| |
| "/" . $task->taskDenominator() . "\n"; |
| |
| } |
| |
| |
| |
| function reverse_complete($task) |
| |
| { |
| |
| echo "COMPLETE: " . $task->unique() . ", " . $task->data() . "\n"; |
| |
| } |
| |
| |
| |
| ?> |
GearmanWorker.php
| <?php |
| |
| |
| |
| |
| |
| |
| |
| echo "Starting\n"; |
| |
| |
| |
| |
| |
| $gmworker= new GearmanWorker(); |
| |
| |
| |
| |
| |
| $gmworker->addServer(); |
| |
| |
| |
| |
| |
| $gmworker->addFunction("reverse", "reverse_fn"); |
| |
| |
| |
| print "Waiting for job...\n"; |
| |
| while($gmworker->work()) |
| |
| { |
| |
| if ($gmworker->returnCode() != GEARMAN_SUCCESS) |
| |
| { |
| |
| echo "return_code: " . $gmworker->returnCode() . "\n"; |
| |
| break; |
| |
| } |
| |
| } |
| |
| |
| |
| function reverse_fn($job) |
| |
| { |
| |
| echo "Received job: " . $job->handle() . "\n"; |
| |
| |
| |
| $workload = $job->workload(); |
| |
| $workload_size = $job->workloadSize(); |
| |
| |
| |
| echo "Workload: $workload ($workload_size)\n"; |
| |
| |
| |
| |
| |
| for ($x= 0; $x < $workload_size; $x++) |
| |
| { |
| |
| echo "Sending status: " . ($x + 1) . "/$workload_size complete\n"; |
| |
| $job->sendStatus($x+1, $workload_size); |
| |
| $job->sendData(substr($workload, $x, 1)); |
| |
| sleep(1); |
| |
| } |
| |
| |
| |
| $result= strrev($workload); |
| |
| echo "Result: $result\n"; |
| |
| |
| |
| |
| |
| return $result; |
| |
| } |
| |
| |
| |
| ?> |
命令行执行后分别输出:
| $ /usr/local/php/bin/php GearmanClient.php |
| STATUS: 1, H:hubery-VirtualBox:2 - 1/12 |
| STATUS: 1, H:hubery-VirtualBox:2 - 2/12 |
| STATUS: 1, H:hubery-VirtualBox:2 - 3/12 |
| STATUS: 1, H:hubery-VirtualBox:2 - 4/12 |
| STATUS: 1, H:hubery-VirtualBox:2 - 5/12 |
| STATUS: 1, H:hubery-VirtualBox:2 - 6/12 |
| STATUS: 1, H:hubery-VirtualBox:2 - 7/12 |
| STATUS: 1, H:hubery-VirtualBox:2 - 8/12 |
| STATUS: 1, H:hubery-VirtualBox:2 - 9/12 |
| STATUS: 1, H:hubery-VirtualBox:2 - 10/12 |
| STATUS: 1, H:hubery-VirtualBox:2 - 11/12 |
| STATUS: 1, H:hubery-VirtualBox:2 - 12/12 |
| COMPLETE: 1, !dlroW olleH |
| DONE |
| |
| $ /usr/local/php/bin/php GearmanWorker.php |
| Starting |
| Waiting for job... |
| Received job: H:hubery-VirtualBox:1 |
| Workload: !dlroW olleH (12) |
| Sending status: 1/12 complete |
| Sending status: 2/12 complete |
| Sending status: 3/12 complete |
| Sending status: 4/12 complete |
| Sending status: 5/12 complete |
| Sending status: 6/12 complete |
| Sending status: 7/12 complete |
| Sending status: 8/12 complete |
| Sending status: 9/12 complete |
| Sending status: 10/12 complete |
| Sending status: 11/12 complete |
| Sending status: 12/12 complete |
| Result: Hello World! |
| Received job: H:hubery-VirtualBox:2 |
| Workload: Hello World! (12) |
| Sending status: 1/12 complete |
| Sending status: 2/12 complete |
| Sending status: 3/12 complete |
| Sending status: 4/12 complete |
| Sending status: 5/12 complete |
| Sending status: 6/12 complete |
| Sending status: 7/12 complete |
| Sending status: 8/12 complete |
| Sending status: 9/12 complete |
| Sending status: 10/12 complete |
| Sending status: 11/12 complete |
| Sending status: 12/12 complete |
| Result: !dlroW olleH |
| |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
· 《HelloGitHub》第 106 期
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 数据库服务器 SQL Server 版本升级公告
· 深入理解Mybatis分库分表执行原理
· 使用 Dify + LLM 构建精确任务处理应用