PHP安装threads多线程扩展基础教程
2018年6月26日一、下载pthreads扩展
下载地址:http://windows.php.net/downloads/pecl/releases/pthreads
二、判断PHP是ts还是nts版
通过phpinfo(); 查看其中的 Thread Safety 项,这个项目就是查看是否是线程安全,如果是:enabled,一般来说应该是ts版,否则是nts版。
三、根据PHP ts\nts版选择对应pthreads的版本
本人php版本是5.4.17的所以下载php_pthreads-0.1.0-5.4-ts-vc9-x86.zip文件包,其中0.1.0表示为当前pthreads版本号,5.4为php版本号,ts就是之前判断php对应的ts、nts版,vs9代表是Visual Studio 2008 compiler编译器编译的,最后的x86代表的是32位的版本。
四、下载pthreads扩展
下载地址:http://windows.php.net/downloads/pecl/releases/pthreads
五、安装pthreads扩展
复制php_pthreads.dll 到目录 bin\php\ext\ 下面。
复制pthreadVC2.dll 到目录 bin\php\ 下面。
复制pthreadVC2.dll 到目录 bin\apache\bin 下面。
复制pthreadVC2.dll 到目录 C:\windows\system32 下面。
打开php配置文件php.ini。在后面加上extension=php_pthreads.dll
六、添加thread类
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 74 75 76 77 78 79 80 81 82 83 84 |
<?php /** * Class test_thread_run * 定义一个多线程的类 */ 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); //这里主要处理数据 } } } /** * @param $urls_array * @return mixed * 这个方法主要调用test_thread_run这个线程类 */ function model_thread_result_get($urls_array) { foreach ($urls_array as $key => $value) { $thread_array[$key] = new test_thread_run($value["url"]); #新建一个对象传入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; } /** * @param $url * @param string $userAgent * @return mixed * 使用phpcurl开始访问百度,访问的方法 */ 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; } /** * 创建url数组 */ 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 "d".($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 "f".($e - $t) . "\n"; |
官方配置是这么配置的,但是通过apache启动不了,如果你wamp启动不了请注释D:\wamp64\bin\apache\apache2.4.23\bin\php.ini
的extension=php_pthreads.dll,这个是和图标上的phpini关联的。添加D:\wamp64\bin\php\php7.0.10\php.ini
在后面加上extension=php_pthreads.dll,重启,这样可以通过命令行执行多线程,请使用php -i 查看php是否有ptherads这个选项,如果有请使用命令行执行该脚本.
作者也没有通过web启动ptherads,如有成功请留言