傻瓜式apt-get 安装的php不支持phpthreads,搜索好久得到的答案是:使用源码编译安装。
下面开始:
1.下载php源码和phpthread源码,然后解压. 使用的php版本是5.6.12,phpthreads版本是2.0.9
cd / usr / local / src wget http : //uk1.php/get/php-5.6.12.tar.gz/from/this/mirror -O php.tar.gz tar xzvf php . tar . gz cd php - 5.6.12 / ext wget https://github/krakjoe/pthreads/archive/v2.0.9.tar.gz tar xzvf v2.0.9.tar.gz2.安装一些依赖包
apt - get install libmemcached - dev libmcrypt - dev libcurl4 - openssl - dev libgd2 - xpm - dev \ libmysqlclient - dev zlib1g - dev libmongo - client - dev libssl1 . 0.0 - dbg \ libssl - dev libssl - dev libsslcommon2 - dev libgd2 - xpm - dev
. / configure \ -- prefix = / usr / local / src / php - 5.6.12 \ -- with - libdir = / lib / x86_64 - linux - gnu / \ -- with - openssl = / usr -- with - curl = / usr \ -- disable - cgi \ -- with - config - file - path = : / usr / local / src / php - 5.6.12 / etc \ -- enable - gd - native - ttf -- enable - mysqlnd \ -- enable - opcache -- enable - pcntl \ -- enable - debug -- enable - maintainer - zts \ -- enable - pthreads -- enable - mbstring \ -- enable - bcmath -- enable - exif \ -- enable - ftp -- enable - shmop \ -- enable - soap -- enable - sockets \ -- enable - sysvmsg -- enable - sysvsem \ -- enable - sysvshm -- enable - wddx \ -- enable - opcache -- enable - zip -- enable - dba
make make install
4.把 extension = "pthreads.so"加到php.ini里 cd / usr / local / src / php - 5.6.12 mkdir etc cp php . ini - production etc / php . ini vi etc/php.ini
然后把 extension = "pthreads.so"加到最后面。
接下来是编译生成pthreads.so
cd /usr/local/src/php-5.6.12/ext/pthreads-2.0.9
/usr/local/src/php-5.6.12/bin/phpize
./configure --with-php-config=/usr/local/src/php-5.6.12/bin/php-config
make
make install
然后创建一个软链接,更改php的默认执行路径:
ln -s /usr/bin/php /usr/local/src/php-5.6.12/sapi/cli/php
查看php安装的模块:
php -m
如果列出来的模块有phpthread,那么就安装成功了。
使用phpthreads:
<?php
class Request extends Thread {
public $url;
public $data;
public function __construct($url) {
$this->url = $url;
}
public function run() {
// 线程处理一个耗时5秒的任务
for($i=0;$i<5;$i++) {
echo '线程: '.date('H:i:s')."\n";
sleep(1);
}
$response = file_get_contents($this->url);
if ($response) {
$this->data = array($response);
}
echo "线程: 任务完成\n";
}
}
$request = new Request('hello.html');
// 运行线程:start()方法会触发run()运行
if ($request->start()) {
// 主进程处理一个耗时10秒的任务,此时线程已经工作
for($i=0;$i<10;$i++) {
echo '进程: '.date('H:i:s')."\n";
sleep(1);
}
// 同步线程并输出线程返回的数据
$request->join();
echo '线程返回数据: '.$request->data[0];
}
更多推荐
为了使用phpthreads,在Ubuntu上重新编译php源码和phpthread源码
发布评论