centos6.9にphp7.0とpthreadを導入して並列処理を実現

Pocket

PHPで並列処理を実現したい

phpは基本的にシングル処理を得意としており、マルチスレッドによる並列処理は苦手とされている。

が、しかし。

PHPで作成したシステムであれば、同じPHPでバッチ処理を作りたくなるケースは普通にありえる。

ただ、シングルスレッドだと性能改善に限界があるため、並列化して爆速化を計りたい。

↓を参考にしました。わかりやすかったです。

PHPはマルチスレッド化で爆速化できるか? pthreadsの使い方(基礎編)

php7.0.9 手動インストール

php7.1ではpthreadが正常にインストールできなかったので、php7.0をインストール。pthreadを使えるようにするには、phpのインストール時に「–enable-maintainer-zts 」オプションの付与が必要。

yumでインストールをすると細かいオプションの指定が出来ないため、敢えて手動でインストール。

phpの公式よりphp7.0.9.tar.gzをダウンロードをして任意の場所に展開。展開後は『configure→make→make install』のお決まりのコマンドで導入する。

▼php公式

http://jp2.php.net/downloads.php

▼configuire コマンド 例

./configure –prefix=/usr –enable-maintainer-zts –with-pear –with-config-file-path=/etc –with-readline –with-zlib –enable-mbstring –with-curl –enable-zip –enable-sockets –enable-sysvsem –enable-sysvshm –with-mhash –with-pcre-regex –with-gettext –enable-bcmath –enable-libxml –enable-json –with-openssl –enable-pcntl –enable-simplexml –enable-xml –enable-xmlreader –with-xmlrpc –enable-xmlwriter –enable-opcache

configureやmake中に様々なライブラリ不足でエラーになった場合は、都度インストールする。

以下は一例。

yum -y install curl-devel
yum -y install curl
yum -y install libmcrypt-devel
yum -y install readline-devel

ちなみに、configureをやり直す場合。

make distclean

コマンドを打ってからやり直したほうがいいらしい。

最後に、phpのバージョンを確認する。

root> php -v
PHP 7.0.9 (cli) (built: Sep 4 2017 20:40:06) ( ZTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

pthread導入

phpが正常にインストールできたら、pthreadを導入。pthreadはphpの拡張機能としてインストールすることが出来ないので、個別にインストールをする。

↓pthread公式サイトから取得

https://pecl.php.net/package/pthreads

php同様、サーバー内の任意のディレクトリにダウンロードして、展開する。展開後はお決まりのコマンドでインストール。

↓pthreadインストール

root> cd ./pthreads-3.1.6
root> phpize
root> ./configure
root> make
root> make install

インストール後に出力される、pthread.soをphp.iniから読み込む。

; If you only provide the name of the extension, PHP will look for it in its
; default extension directory.

extension=pthreads.so

phpinfoでpthreadが読み込まれていれば導入完了。

導入環境

今回の環境は「さくらVPS」上で作成しています。

安価でroot権限付いてくるし、かなり重宝します。

「 さくらVPS」は↓のバナーから詳細をご覧いただけます。


IT/パソコン

Posted by wpmaster