PHP 7 multi-thread building tutorial, PHP 7 multi-thread Building

Source: Internet
Author: User
Tags php source code zts

PHP 7 multi-thread building tutorial, PHP 7 multi-thread Building

I always wanted to write a crawler, so I wrote one in PHP. As we all know, PHP does not support multithreading, so the crawling efficiency is very low. It runs 10 processes at the same time, and the memory and CPU usage are very high, about 10 processes were opened to crawl and the crawling time was optimized, but my mac pro was almost down. As a result, PHP7 and pthreads multi-thread tools were adopted. After tests, the stability, efficiency, memory and cpu usage were satisfactory. The following describes how to build a multi-threaded PHP 7 environment.

Install PHP7

All of the following commands are run and downloaded under the root user. The latest version is 7.0.3. You can select your own version based on your needs. PHP5.4 and later versions are also supported.

wget http://ar2.php.net/get/php-7.0.3.tar.gz/from/this/mirror -O php.tar.gztar xvfz php.tar.gzcd php

Compile and install.

Option to allow multiple PHP versions to coexist. However, maintainer-zts is required. If you only use crawlers, you do not need to install other extensions except curl. If the installation fails, find and solve the problem by yourself. For example, if iconv is not found during installation, then without is used directly.

./configure --prefix=/usr/local/php7 --without-iconv --enable-maintainer-zts --with-curlmakemake install

Installation Complete

Then install pthreads. Installing with pecl is super simple.

cd /usr/local/php7/bin./pecl install pthreads

Installation Complete

Configuration File

Create a php file in the/usr/local/php7/lib/directory. INI file. The initial file can be found in the root directory of the php source code. ini-production and then edit php. ini, add at the end

extension="pthreads.so"

Installation Complete

Check whether the installation is successful

Run

/usr/local/php7/bin/php -m

View the 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();?>

Hello World is successfully output.

The above is a PHP 7 multi-threading tutorial introduced by xiaobian. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.