您的位置:365bet体育备用网址器 > 应用 > else if($pid){ //父进程处理逻辑,可以用php实现多进

else if($pid){ //父进程处理逻辑,可以用php实现多进

2019-08-16 20:29

由于php的经过是不扶助二十八线程的,有些场景为了方便以及进步品质,能够用php完毕多进度以弥补那一个不足:

PHP咋样落到实处多进程?

复制代码 代码如下:

PHP实现多进度并行实行脚本

#!/usr/bin/env php
<?php
$cmds=array(
        array('/apps/bin/launcher.php','charge/promotion_props_stat.php','mobile',1),
        array('/apps/bin/launcher.php','charge/promotion_props_stat.php','mobile',2),
        array('/apps/bin/launcher.php','charge/promotion_props_stat.php','click',1),
        array('/apps/bin/launcher.php','charge/promotion_props_stat.php','click',2),
        array('/apps/bin/launcher.php','charge/promotion_props_stat.php',1),
        array('/apps/bin/launcher.php','charge/promotion_props_stat.php',2)
);

出于php的经过是不支持二十四线程的,有个别场景为了便利以及狠抓品质,能够用php达成多进度以弥补那么些不足:

foreach($cmds as $cmd){
        $pid=pcntl_fork();
        if($pid==-1){ //进程创制失利
                die('fork child process failure!');
        }
        else if($pid){ //父进度管理逻辑
                pcntl_wait($status,WNOHANG);
        }
        else{ //子进度管理逻辑
                pcntl_exec('/usr/local/bin/php',$cmd);
        }
}

 

您大概感兴趣的稿子:

  • PHP多进度编制程序实例
  • 深深研讨PHP的多进程编制程序方法
  • PHP使用pcntl_fork实现多进程下载图片的方法
  • Linux下完毕PHP多进度的点子分享
  • PHP的pcntl多进程用法实例
  • PHP CLI情势下的多进度应用分析
  • 以实例周密授课PHP中多进程编制程序的有关函数的应用
  • PHP达成多进度并行操作的详解(可做护理进度)
  • php中完结进度锁与多进度的章程
  • PHP并发多进度管理利器Gearman使用介绍
  • PHP多进程编制程序总计(推荐)
  • PHP多进度编制程序实例详解

#!/usr/bin/env php

<?php

$cmds=array(

array('/apps/bin/launcher.php','charge/promotion_props_stat.php','mobile',1),

array('/apps/bin/launcher.php','charge/promotion_props_stat.php','mobile',2),

array('/apps/bin/launcher.php','charge/promotion_props_stat.php','click',1),

array('/apps/bin/launcher.php','charge/promotion_props_stat.php','click',2),

array('/apps/bin/launcher.php','charge/promotion_props_stat.php',1),

array('/apps/bin/launcher.php','charge/promotion_props_stat.php',2)

);

 

foreach($cmds as $cmd){

$pid=pcntl_fork();

if($pid==-1){ //进度创制战败

die('fork child process failure!');

}

else if($pid){ //父进度管理逻辑

pcntl_wait($status,WNOHANG);

}

else{ //子进度管理逻辑

pcntl_exec('/usr/local/bin/php',$cmd);

}

}

 

PHP 5.3 以上版本,使用pthreads PHP扩充,能够使PHP真正地协理十二线程。二十四线程在管理重复性的轮回职务,能够大大缩小程序推行时间。

   作者在此之前的篇章中说过,大相当多网址的质量瓶颈不在PHP服务器上,因为它能够轻巧地通过横向扩充服务器或CPU核数来轻易应对(对于各个云主机,扩充VPS或CPU核数就更便于了,直接以备份镜像扩展VPS,连操作系统、意况都不用安装配备),而是在乎MySQL数据库。要是用 MySQL 数据库,一条联合查询的SQL,可能就足以管理完专门的学问逻辑,但是,蒙受多量出现央浼,就歇菜了。假诺用 NoSQL 数据库,可能要求12回询问,技能管理完同样地专门的工作逻辑,但每趟查询都比 MySQL 要快,拾一回循环NoSQL查询或然比叁回MySQL联合查询越来越快,应对几万次/秒的查询完全没难题。借使加上PHP多线程,通过十二个线程同期查询 NoSQL,重返结果汇总输出,速度将要越来越快了。我们实在的应用程式产品中,调用二个透过用户喜好实时推荐商品的PHP接口,PHP须求对BigSea NoSQL数据库发起500~一千次询问,来实时算出用户的特性喜好物品数量,PHP八线程的效劳非常分明。

  PHP扩充下载:
  PHP手册文书档案:

  1、增添的编写翻译安装(Linux),编辑参数 --enable-maintainer-zts 是必选项:

cd /Data/tgz/php-5.5.1
./configure --prefix=/Data/apps/php --with-config-file-path=/Data/apps/php/etc --with-mysql=/Data/apps/mysql --with-mysqli=/Data/apps/mysql/bin/mysql_config --with-iconv-dir --with-freetype-dir=/Data/apps/libs --with-jpeg-dir=/Data/apps/libs --with-png-dir=/Data/apps/libs --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt=/Data/apps/libs --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --enable-opcache --with-pdo-mysql --enable-maintainer-zts
make clean
make
make install        

unzip pthreads-master.zip
cd pthreads-master
/Data/apps/php/bin/phpize
./configure --with-php-config=/Data/apps/php/bin/php-config
make
make install

 

vi /Data/apps/php/etc/php.ini

 

添加:

extension = "pthreads.so"

 2、给出一段PHP二十八线程、与For循环,抓取百度找出页面包车型大巴PHP代码示例:

文件1

<?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" => "));

}

 

$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";

?>

 

文件2

<?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" => "));

}

 

$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";

?>

 

互连网看看的,未有看太懂,先转了!

作品来源:刘俊涛的博客

地址:

招待关切,有标题一并读书应接留言、商议。

本文由365bet体育备用网址器发布于应用,转载请注明出处:else if($pid){ //父进程处理逻辑,可以用php实现多进

关键词: