可谓史上最简单的PHP多线程支持部署方案
基本说明
系统:CentOS 6.9 x64
除pthreads模块为编译安装外,全部使用yum安装
第一步:安装remi源,并启用php 7.2
yum install http://rpmfind.net/linux/remi/enterprise/6/remi/x86_64/remi-release-6.9-2.el6.remi.noarch.rpm
yum-config-manager --disable remi-php56
yum-config-manager --enable remi-php72
注1:请访问 http://rpmfind.net/linux/rpm2html/search.php?query=remi 选择你对应的版本
注2:yum-conifg-manager 不可用的话,请自行百度
第二步:安装PHP 7.2.4
yum install php php-fpm php-mysqli php-redis php-pecl php-opcache php-gd php-mcrypt php-common php-xml php-devel php-cli php-mbstring php-process
需要的扩展,请按需选择,配置pthreads的话,只要php即可
第三步:安装pthreads(重点)
//安装
wget https://codeload.github.com/krakjoe/pthreads/zip/master
unzip master
cd pthreads-master/
/usr/bin/zts-phpize
./configure --with-php-config=/usr/bin/zts-php-config
make && make install
//配置
echo extension = pthreads.so >> /etc/php-zts.d/50-pthreads.ini
就是这么简单
坑点:
1、不要用pecl安装pthreads
2、不要从git的releases里下载pthreads源码
3、按pthreads官方文档去做,但是要注意php是remi源上的,默认提供了php-zts的支持,只要在标准的php程序前加上zts执行
验证和使用:
1、确认扩展存在
zts-php -m| grep pthreads
2、测试代码
将下面代码存为PHP文件test.php
<?php
$thread = new class extends Thread {
public function run() {
echo "Hello World\n";
}
};
$thread->start() && $thread->join();
?>
执行
zts-php test.php
结果有Hello World输出即成功