W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
我們?cè)谧鲰?xiàng)目的時(shí)候,有些需求,特別是數(shù)據(jù)的響應(yīng)處理需要花費(fèi)大量的時(shí)間,由于php是一個(gè)短生命周期的腳本語(yǔ)言,到了默認(rèn)的30秒,php的數(shù)據(jù)處理還沒(méi)完成,php的生命周期就結(jié)束了。這時(shí)需要使用異步并發(fā)處理策略,也就是說(shuō),一次php調(diào)用可以發(fā)出的多個(gè)請(qǐng)求,這些請(qǐng)求不是按照順序執(zhí)行,而是可以異步并發(fā)執(zhí)行的,一些請(qǐng)求用于在后臺(tái)處理數(shù)據(jù),一些請(qǐng)求用于接受后臺(tái)響應(yīng)狀態(tài),根據(jù)狀態(tài),與用戶做一些簡(jiǎn)單的交互。但是問(wèn)題來(lái)了,我們都知道php本身是不支持多線程的,那么應(yīng)該怎么實(shí)現(xiàn)php的多線程呢?
1、linux下的php多線程
下面所講的東西是源自php的pcntl_fork函數(shù).因?yàn)檫@個(gè)函數(shù)依賴操作系統(tǒng)fork的實(shí)現(xiàn),所以本文所講的東西只適用于linux/unix。那么先看看這個(gè)函數(shù)的用法吧.php手冊(cè)上是這么說(shuō)的:
<?php
$pid = pcntl_fork();
if ($pid == -1) {
die('could not fork');
} else if ($pid) {
// we are the parent
pcntl_wait($status); //Protect against Zombie children
} else {
// we are the child
}
?>
通過(guò)pcntl_fork創(chuàng)建一個(gè)子進(jìn)程,如果返回值是-1的話,那么說(shuō)明子進(jìn)程創(chuàng)建失敗.創(chuàng)建成功的進(jìn)程id會(huì)返回給父進(jìn)程,0返回給子進(jìn)程.不好理解吧,所以應(yīng)該這樣寫:
<?php
$pid = pcntl_fork();
if($pid == -1){
//創(chuàng)建失敗咱就退出唄,沒(méi)啥好說(shuō)的
die('could not fork');
}
else{
if($pid){
//從這里開(kāi)始寫的代碼是父進(jìn)程的,因?yàn)閷懙氖窍到y(tǒng)程序,記得退出的時(shí)候給個(gè)返回值
exit(0);
}
else{
//從這里開(kāi)始寫的代碼都是在新的進(jìn)程里執(zhí)行的,同樣正常退出的話,最好也給一個(gè)返回值
exit(0);
}
}
?>
這樣一改好理解多了,如果你父進(jìn)程希望知道子進(jìn)程正常退出的話,可以加上前面的pcntl_wait。2.通過(guò)stream_socket_client 方式
function sendStream() {
$english_format_number = number_format($number, 4, '.', '');
echo $english_format_number;
exit();
$timeout = 10;
$result = array();
$sockets = array();
$convenient_read_block = 8192;
$host = "test.local.com";
$sql = "select waybill_id,order_id from xm_waybill where status>40 order by update_time desc limit 1 ";
$data = Yii::app()->db->createCommand($sql)->queryAll();
$id = 0;
foreach ($data as $k => $v) {
if ($k % 2 == 0) {
$send_data[$k]['body'] = NoticeOrder::getSendData($v['waybill_id']);
} else {
$send_data[$k]['body'] = array($v['order_id'] => array('extra' => 16));
}
$data = json_encode($send_data[$k]['body']);
$s = stream_socket_client($host . ":80", $errno, $errstr, $timeout, STREAM_CLIENT_ASYNC_CONNECT | STREAM_CLIENT_CONNECT);
if ($s) {
$sockets[$id++] = $s;
$http_message = "GET /php/test.php?data=" . $data . " HTTP/1.0\r\nHost:" . $host . "\r\n\r\n";
fwrite($s, $http_message);
} else {
echo "Stream " . $id . " failed to open correctly.";
}
}
while (count($sockets)) {
$read = $sockets;
stream_select($read, $w = null, $e = null, $timeout);
if (count($read)) {
/* stream_select generally shuffles $read, so we need to
compute from which socket(s) we're reading. */
foreach ($read as $r) {
$id = array_search($r, $sockets);
$data = fread($r, $convenient_read_block);
if (strlen($data) == 0) {
echo "Stream " . $id . " closes at " . date('h:i:s') . ".<br> ";
fclose($r);
unset($sockets[$id]);
} else {
$result[$id] = $data;
}
}
} else {
/* A time-out means that *all* streams have failed
to receive a response. */
echo "Time-out!\n";
break;
}
}
print_r($result);
}
3、通過(guò)多進(jìn)程代替多線程
function daemon($func_name,$args,$number){
while(true){
$pid=pcntl_fork();
if($pid==-1){
echo "fork process fail";
exit();
}elseif($pid){//創(chuàng)建的子進(jìn)程
static $num=0;
$num++;
if($num>=$number){
//當(dāng)進(jìn)程數(shù)量達(dá)到一定數(shù)量時(shí)候,就對(duì)子進(jìn)程進(jìn)行回收。
pcntl_wait($status);
$num--;
}
}else{ //為0 則代表是子進(jìn)程創(chuàng)建的,則直接進(jìn)入工作狀態(tài)
if(function_exists($func_name)){
while (true) {
$ppid=posix_getpid();
var_dump($ppid);
call_user_func_array($func_name,$args);
sleep(2);
}
}else{
echo "function is not exists";
}
exit();
}
}
}
function worker($args){
//do something
}
daemon('worker',array(1),2);
下一頁(yè)找到版本2的
下載下來(lái),這個(gè)v2 才是php5才可以使用的
下載下來(lái),安裝:
或者,您直接這樣下載:
cd /tools
wget https://github.com/krakjoe/pthreads/archive/v2.0.10.zip
unzip v2.0.10.zip
cd pthreads-2.0.10
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
注意:您的php 在編譯的時(shí)候需要開(kāi)啟 –enable-maintainer-zts./configure --prefix=/usr/local/php --disable-fileinfo --enable-fpm --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-openssl --with-zlib --with-curl --enable-ftp --with-gd --with-xmlrpc --with-jpeg-dir --with-png-dir --with-freetype-dir --enable-gd-native-ttf --enable-mbstring --with-mcrypt=/usr/local/libmcrypt --enable-zip --with-mysql=/usr/local/mysql --without-pear --enable-maintainer-zts
vim /etc/php.ini
添加
extension=pthreads.so
重啟php
/etc/init.d/php-fpm restart
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: