php多线程示例 PHP开启多线程扩展方法
php从5.3开始支持多线程pthreads扩展,并同时支持window和linux。
pthreads可以实现多任务同步或异步执行,它提供了创建多线程应用所需的全套工具,默认未安装。
安装
安装前需要确认PHP版本及安装位数,可使用phpinfo(); Architecture即是当前安装位数。官方PHP在5.6及以前版本均为32位(兼容64位),所以PHP5可直接下载32位pthreads扩展。
说明:3.0版本及以上为PHP7专用,3.0版本以下为PHP5专用。
这里以PHP5.5(32位)为例安装:
- 选择2.0.9版本目录
- 下载 php_pthreads-2.0.9-5.5-ts-vc11-x86.zip 包并解压
注意:包名规则中已经包含了pthreads的版本+php版本+底层编译VC版本,所以下载时可以按这个规则去找。
- 复制php_pthreads.dll到PHP扩展目录中。复制pthreadVC2.dll到PHP安装根目录和apache安装bin目录下)
- 打开php.ini配置文件,增加扩展 extension=php_pthreads.dll 保存并退出,重启服务器
- 写代码测试。
没有报错即安装成功,如果有报错提示没有扩展可先phpinfo();查看下扩展是否安装或查看配置文件是否修改错。
进入PHP安装bin目录下执行命令:
如果安装失败可尝试安装低版本,安装成功后需要修改php.ini文件,增加 extension=pthreads.so ,以开启pthreads扩展。
测试与window类似。
应用
在官方文档中已经有详细的说明,具体的可以查看文档中 PHP手册 >> 函数参考 >> 进程控制扩展 >> pthreads
注意:PHP资源类型( PHP 中很多使用到 Resource 资源类型的扩展或函数并未针对多线程场景进行特殊设计)在多线程中共享会存在很多问题,大部分资源类型无法共享如数据库连接,文件资源等,只有socket,mysqli 等少量资源类型可以共享!
很多示例可以在github上查看:https://www.github.com/krakjoe/pthreads examples目录中。
示例:::
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | <?php class test_thread_run extends Thread { public $url; public $data; public function __construct($url) { $this->url = $url; } public function run() { if(($url = $this->url)) { $this->data = model_http_curl_get($url); } } } function model_thread_result_get($urls_array) { foreach ($urls_array as $key => $value) { $thread_array[$key] = new test_thread_run($value["url"]); $thread_array[$key]->start(); } foreach ($thread_array as $thread_array_key => $thread_array_value) { while($thread_array[$thread_array_key]->isRunning()) { usleep(10); } if($thread_array[$thread_array_key]->join()) { $variable_data[$thread_array_key] = $thread_array[$thread_array_key]->data; } } return $variable_data; } function model_http_curl_get($url,$userAgent="") { $userAgent = $userAgent ? $userAgent : 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2)'; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_TIMEOUT, 5); curl_setopt($curl, CURLOPT_USERAGENT, $userAgent); $result = curl_exec($curl); curl_close($curl); return $result; } for ($i=0; $i < 100; $i++) { $urls_array[] = array("name" => "baidu", "url" => "http://www.baidu.com/s?wd=".mt_rand(10000,20000)); } $t = microtime(true); $result = model_thread_result_get($urls_array); $e = microtime(true); echo "多线程:".($e-$t)."\n"; $t = microtime(true); foreach ($urls_array as $key => $value) { $result_new[$key] = model_http_curl_get($value["url"]); } $e = microtime(true); echo "For循环:".($e-$t)."\n"; ?> |
文章来源:
http://blog.51cto.com/php2012web/1897614
https://www.cnblogs.com/kluan/p/5934228.html




布施恩德可便相知重
微信扫一扫打赏
支付宝扫一扫打赏