Cara menggunakan php 7 multi-threading

View Discussion

Table of Contents

  • Related Posts
  • Requirements
  • Video Runthrogh
  • Recompile PHP
  • Add Pthreads
  • Multithreading Test
  • How do I enable thread safety?
  • How do I enable ZTS PHP?
  • Do I need thread safe PHP?
  • What is PHP ZTS?

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Thread-safe: It is used to ensure that when the shared data structure which is manipulated by different threads are prevented from entering the race condition. Thread-safety is recommended when the web server run multiple threads of execution simultaneously for different requests. In Thread Safety binary can work in a multi-threaded web server context. Thread Safety works by creating a local storage copy in each thread so that the data will not collide with another thread.
    For example:

    • Apache + LoadModule
    • IIS

    Non-thread-safe: It does not check the safety of the threads which makes it faster to run but at the same time, it becomes more unstable and crashes very frequently. It refers to a single thread only builds. In non-thread safe version binaries widespread use in the case of interaction with a web server through the FastCGI protocol, by not utilizing multi-threading.
    For example:

    • Apache + FastCGI
    • IIS + FastCGI

    So it depends on the way that you want to use PHP. AFAIR running PHP with the fastCGI is the preferable way. If you are unknown which version of PHP is installed in your system then there is an easy way to know that.
    Check the version of installed PHP Thread safe or Non Thread Safe:
    Open a phpinfo() and search for the line Thread safety for a thread-safe build you should find enable.

    • On Windows:php -i|findstr "Thread"
    • On *nix:php -i|grep Thread
    • In the both cases will display any oneThread Safety => enabled //or Thread Safety => disabled

    PHP can support multithreading for compute intensive workloads. This tutorial will recompile PHP from source with ZTS enabled in order to allow us to then add the pthreads extension for the PHP CLI (not for Apache or FPM). I will not dive into when not to use multithreading in PHP, but its fair to say that there are good reasons why it supported for the webserver, and hence the pre-built package that you install from the Ubuntu repositories.

    The majority of the content in the answer below came from an answer on Stack Overflow for how to install php7 (zts) + pthreads on Ubuntu 14.04. However I have tweaked it as necessary for Ubuntu 16.04 and without even trying to add FPM support.

    This tutorial will set up the latest version of PHP from the source code. This is not a release, and thus should not be used in production. This tutorial will form a foundation on which I will create production suitable versions in future, using releases of PHP.

    Related Posts

    • Install PHP 7.0 With Pthreads on Ubuntu 16.04

    Requirements

    • 1GiB Ram - lower than this and your compilation step may fail

    Video Runthrogh

    Recompile PHP

    Download the necessary packages for compilation.

    sudo apt update && \ sudo apt install -y libzip-dev bison autoconf build-essential pkg-config git-core \ libltdl-dev libbz2-dev libxml2-dev libxslt1-dev libssl-dev libicu-dev \ libpspell-dev libenchant-dev libmcrypt-dev libpng-dev libjpeg8-dev \ libfreetype6-dev libmysqlclient-dev libreadline-dev libcurl4-openssl-dev

    Remove any existing php7 and recreate php7 and other subdirectories

    sudo rm -rf /etc/php7 sudo mkdir -p /etc/php7 sudo mkdir -p /etc/php7/cli sudo mkdir -p /etc/php7/etc

    Go to your home directory. And download the PHP source code that we will compile PHP from.

    cd $HOME git clone //github.com/php/php-src.git --depth=1

    Download the pthreads source to the extensions folder.

    cd $HOME/php-src/ext git clone //github.com/krakjoe/pthreads -b master pthreads cd $HOME/php-src ./buildconf --force CONFIGURE_STRING="--prefix=/etc/php7 --with-bz2 --with-zlib --enable-zip --disable-cgi \ --enable-soap --enable-intl --with-openssl --with-readline --with-curl \ --enable-ftp --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd \ --enable-sockets --enable-pcntl --with-pspell --with-enchant --with-gettext \ --with-gd --enable-exif --with-jpeg-dir --with-png-dir --with-freetype-dir --with-xsl \ --enable-bcmath --enable-mbstring --enable-calendar --enable-simplexml --enable-json \ --enable-hash --enable-session --enable-xml --enable-wddx --enable-opcache \ --with-pcre-regex --with-config-file-path=/etc/php7/cli \ --with-config-file-scan-dir=/etc/php7/etc --enable-cli --enable-maintainer-zts \ --with-tsrm-pthreads --enable-debug --enable-fpm \ --with-fpm-user=www-data --with-fpm-group=www-data" ./configure $CONFIGURE_STRING

    Build PHP.

    make && sudo make install

    Add Pthreads

    We are going to manually compile and add pthreads to PHP, so make the phpize and php-config helper programs executable

    sudo chmod o+x /etc/php7/bin/phpize sudo chmod o+x /etc/php7/bin/php-config

    Run phpize on pthreads.

    cd $HOME/php-src/ext/pthreads* /etc/php7/bin/phpize

    Set configuration options for pthreads. --enable-pthreads=shared is the main aspect of the configuration

    ./configure \ --prefix='/etc/php7' \ --with-libdir='/lib/x86_64-linux-gnu' \ --enable-pthreads=shared \ --with-php-config='/etc/php7/bin/php-config'

    Build and install the extension

    make && sudo make install sudo cp php.ini-production /etc/php7/cli/php-cli.ini echo "extension=pthreads.so" | sudo tee -a /etc/php7/cli/php-cli.ini echo "zend_extension=opcache.so" | sudo tee -a /etc/php7/cli/php.ini

    Remove any link to PHP if it already exists. If this was a server from scratch and you only ran the steps in this tutorial, this step should do nothing.

    sudo rm /usr/bin/php

    Link to your newly compiled PHP binary.

    sudo ln -s /etc/php7/bin/php /usr/bin/php

    Testing

    You can check which version of PHP you have installed by running:

    php --version

    Multithreading Test

    In order to test that multithreading was now available in the PHP CLI, I tweaked a script from a tutorial on Sitepoint and put it below

    <?php class Task extends Threaded { private $value; public function __construct(int $i) { $this->value = $i; } public function run() { $s=0; for ($i=0; $i<10000; $i++) { $s++; } echo "Task: {$this->value}\n"; } } # Create a pool of 4 threads $pool = new Pool(4); for ($i = 0; $i < 15000; ++$i) { $pool->submit(new Task($i)); } while ($pool->collect()); $pool->shutdown();

    As you can see below, when I run the script, I have 5 PHP threads, one from the main thread, and then the 4 workers in the pool that I created.

    Conclusion

    We now have support for multithreading in PHP on Ubuntu 16.04. If you wish to learn more about how to make use of pthreads, I recommend reading "Parallel Programming with Pthreads in PHP – the Fundamentals" and the man pages of course.

    References

    • Stack Overflow - how to install php7 (zts) + pthreads on Ubuntu 14.04
    • Stack Overflow - How can one use multi threading in PHP applications
    • Tutorial: Multi-Threading in PHP7 w/ pThreads
    • FLOWL - Compile PHP 5.6 with pthreads and ZTS Ubuntu/Debian
    • Stack Overflow - Recompile PHP with ZTS enabled on Ubuntu

    Last updated: 16th September 2021
    First published: 16th August 2018

    How do I enable thread safety?

    Check the version of installed PHP Thread safe or Non Thread Safe: Open a phpinfo() and search for the line Thread safety for a thread-safe build you should find enable.

    How do I enable ZTS PHP?

    Installation.

    Add the package repository. sudo add-apt-repository ppa:ondrej/php-zts sudo apt-get -y update..

    Install ZTS enabled PHP. ... .

    Check ZTS is enabled. ... .

    Install pThreads via Pecl. ... .

    Check pThreads is installed..

    Do I need thread safe PHP?

    So what do I choose? If you choose to run PHP as a CGI binary, then you won't need thread safety, because the binary is invoked at each request. For multithreaded webservers, such as IIS5 and IIS6, you should use the threaded version of PHP.

    What is PHP ZTS?

    This is used to enable the Zend Thread Safety package in PHP which is used for pthreads. From the PHP manual(//php.net/manual/en/pthreads.requirements.php) pthreads requires a build of PHP with ZTS (Zend Thread Safety) enabled ( --enable-maintainer-zts or --enable-zts on Windows )

    Postingan terbaru

    LIHAT SEMUA