Home > Article > Backend Development > PHP7 multi-threading tutorial
This article mainly introduces the PHP7 multi-thread construction tutorial. Friends who need it can refer to it
I have always wanted to write a crawler, so I wrote one in PHP. As we all know, PHP does not support multi-threading, so The crawling efficiency is very low. 10 processes are running at the same time, and the memory and CPU usage are very high. I opened about 10 processes for crawling and optimized the crawling time, but my mac pro still almost crashed. So I started to use PHP7 combined with pthreads multi-threading tool. After testing, I was very satisfied with the stability, efficiency, memory and CPU usage. The following will introduce the multi-threaded environment construction of PHP7.
Install PHP7
All the following commands are executed under the root user to download PHP7. The latest version is 7.0.3. Select the version according to the situation. , all versions after PHP5.4 are also supported.
wget http://ar2.php.net/get/php-7.0.3.tar.gz/from/this/mirror -O php.tar.gz tar xvfz php.tar.gz cd php
Compile and install.
Options can be selected by yourself, allowing multiple versions of PHP to coexist. But maintainer-zts is necessary. If you only do crawlers, except for curl, other extensions basically do not need to be installed. If you encounter something that cannot be installed, you can find the solution by yourself. For example, when I encountered that iconv was not found during installation, I just did it without.
./configure --prefix=/usr/local/php7 --without-iconv --enable-maintainer-zts --with-curl make make install
Installation completed
Then start installing pthreads. Installation with pecl is super easy.
cd /usr/local/php7/bin ./pecl install pthreads
Installation completed
Configuration file
In /usr Create a new php.ini file in the /local/php7/lib/ directory. The initial file can be found in the root directory of the php source code. php.ini-production Then edit php.ini and add
extension="pthreads.so"# at the end. ##Installation completedCheck whether the installation is successfulRun
/usr/local/php7/bin/php -mView module support list , if pthreads exists, the environment is created successfully. Or edit the file test.php
<?php $thread = new class extends Thread { public function run() { echo "Hello World\n"; } }; $thread->start() && $thread->join(); ?>The successful output of Hello World indicates success. The above is the entire content of this article, I hope it will be helpful to everyone's study.
The performance of destructor method in inheritance in php construction method_php skills
PHP MSSQL paging example_php skills
php remote download class sharing_php skills
The above is the detailed content of PHP7 multi-threading tutorial. For more information, please follow other related articles on the PHP Chinese website!