最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

php如何多线程

来源:动视网 责编:小采 时间:2020-11-27 14:56:04
文档

php如何多线程

php如何多线程:PHP开启多线程的方法php如何安装pthreads的拓展的,我采用的是windows安装,我本机的开发环境是phpstudy。有几点特别需要注意,在window中此类拓展一定是要在线程安全(ts)的php版本中运行。安装1、复制php_pthreads.dll 到目录 bin\php
推荐度:
导读php如何多线程:PHP开启多线程的方法php如何安装pthreads的拓展的,我采用的是windows安装,我本机的开发环境是phpstudy。有几点特别需要注意,在window中此类拓展一定是要在线程安全(ts)的php版本中运行。安装1、复制php_pthreads.dll 到目录 bin\php


PHP开启多线程的方法

php如何安装pthreads的拓展的,我采用的是windows安装,我本机的开发环境是phpstudy。

有几点特别需要注意,在window中此类拓展一定是要在线程安全(ts)的php版本中运行。

安装

1、复制php_pthreads.dll 到目录 bin\php\ext\

2、复制pthreadVC2.dll 到目录 C:\windows\system32 下面。

3、打开php配置文件php.ini,在后面加上extension=php_pthreads.dll。

提示!

Windows系统需要将 pthreadVC2.dll 所在路径加入到 PATH 环境变量中。我的电脑--->鼠标右键--->属性--->高级--->环境变量--->系统变量--->找到名称为Path的--->编辑--->在变量值最后面加上pthreadVC2.dll的完整路径。

测试

测试脚本我复制的是http://zyan.cc/pthreads/这里的实例代码。

这里我贴上我的代码,就是在子线程中我添加了日志记录,判断下他的返回值如何

<?php 
set_time_limit(0);
 class test_thread_run extends Thread 
 { 
 public $url; 
 public $data='init'; 
 
 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); 
 if($result=== false){
 $result = curl_error($curl);
 }
 curl_close($curl);
 $pid = Thread::getCurrentThreadId();
 $file='D:\\pid\\'.$pid.'p.txt';
 
 file_put_contents($file,$result);
 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"; 
 //print_r($result);
 $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"; 
 //print_r($result_new);
?>

结果在我的目录下:

里面的文本内容是:

也就是说子线程是正常调度了,但是没有拿到数据。此类情况可能是我的网速的原因,垃圾8M电信。

推荐教程:PHP视频教程

文档

php如何多线程

php如何多线程:PHP开启多线程的方法php如何安装pthreads的拓展的,我采用的是windows安装,我本机的开发环境是phpstudy。有几点特别需要注意,在window中此类拓展一定是要在线程安全(ts)的php版本中运行。安装1、复制php_pthreads.dll 到目录 bin\php
推荐度:
标签: 如何 php 线程
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top