How to install PHP pthreads extension on Ubuntu? [closed]
Once ZTS PHP is installed you must then install a module called pThreads which adds async functionality to PHP natively.,Prerequisite: a Virtual Machine or server running a clean install of Ubuntu 16.04.,These steps will build PHP with access to the `Thread` class, which in turn gives access to async PHP! Now the fun part…,This code example – edited, but originally taken from here – is a good place to start understanding how pThreads works. It simply creates a loop and spawns child processes designed to take a random amount of time to complete. Once they complete the result is reported.
1. Add the package repository
sudo add-apt-repository ppa:ondrej/php-zts sudo apt-get -y update
pThreads won’t install without the php7.0-zts-xml and php7.0-zts-dev packages, so these must also be installed:
sudo apt -y install php7.0-zts php7.0-zts-common php7.0-zts-xml php7.0-zts-dev
This should return an integer of ‘1’.
4. Install pThreads via Pecl
sudo apt install php-pear sudo pecl install pthreads sudo echo "extension=pthreads.so" >> /etc/php/7.0-zts/mods-available/pthreads.ini sudo echo "extension=pthreads.so" >> /etc/php/7.0-zts/cli/conf.d/pthreads.ini
This should return an integer of ‘1’.
php -r "print_r(class_exists('Thread'));"
This code example – edited, but originally taken from here – is a good place to start understanding how pThreads works. It simply creates a loop and spawns child processes designed to take a random amount of time to complete. Once they complete the result is reported.
#!/usr/bin/php arg = $arg; > public function run() < if ($this->arg) < $sleep = mt_rand(5, 10); printf('%s: %s -start -sleeps %d' . "\n", date("g:i:sa"), $this->arg, $sleep); sleep($sleep); printf('%s: %s -finish' . "\n", date("g:i:sa"), $this->arg); > > > $ops = array(); foreach (range("A", "Z") as $k => $task) < $ops[$task] = new AsyncOperation($task); $ops[$task]->start(); >
The following example achieves this by limiting itself to a given concurrency. Further improvements can be made; adding timeouts to the processes and killing old processes would mean the task pool is better maintained. But this is a good starting place for a proof of concept.
#!/usr/bin/php arg = $arg; > public function run() < if ($this->arg) < $sleep = mt_rand(5, 10); printf('%s: %s -start -sleeps %d' . "\n", date("g:i:sa"), $this->arg, $sleep); sleep($sleep); printf('%s: %s -finish' . "\n", date("g:i:sa"), $this->arg); > > > // Create a tasks list $tasks = range("A", "Z"); // Concurrency setting, exit status and operations array $conc = 2; $exit = false; $ops = array(); while (!$exit) < foreach ($tasks as $k =>$task) < // If the current operations count is less than concurrency setting, add a new task to the pool $pool = count($ops); if ($pool >= $conc) < sleep(1); break; >else < $ops[$task] = new AsyncOperation($task); $ops[$task]->start(); unset($tasks[$k]); > > foreach ($ops as $task => $op) < if (!$op->isRunning()) < // Op finished - remove from pool printf('%s: %s -exited' . "\n", date("g:i:sa"), $task); unset($ops[$task]); >> if (empty($tasks) && empty($ops)) < $exit = true; >>
If you want to install other PHP packages using the package manage then you have to install the ZTS versions from the custom package repo. For example to install curl run this;
sudo apt install php7.0-zts-curl
To check what packages are available to you run:
Answer by Molly Schneider
Congratz! PHP7 with thread safety is installed. Now let’s install pthreads:,Edit: I think I’ll write a little script next time for automated install of PHP7 ZTS with pthreads.,@morphesus I try to follow the commands, as soon as I reach sudo apt-get install php7.0-zts php7.0-zts-dev I get the following error:,There is no way to enable pthreads like other modules. You need to compile PHP thread safe. In the comment I linked I explained a bit how you can do this.
libevent-pthreads-2.0-5 - Asynchronous event notification library (pthreads) libilmbase12 - several utility libraries from ILM used by OpenEXR libpthread-stubs0-dev - pthread stubs not provided by native libc, development files blcr-testsuite - Userspace tools to Checkpoint and Restart Linux processes blcr-util - Userspace tools to Checkpoint and Restart Linux processes lib32cr0 - (32bit) Libraries to Checkpoint and Restart Linux processes libcr-dbg - Libraries to Checkpoint and Restart Linux processes libcr-dev - Development files for BLCR libcr0 - Libraries to Checkpoint and Restart Linux processes libzthread-2.3-2 - Object-oriented synchronization library for C++ libzthread-dev - Object-oriented synchronization library for C++ pbzip2 - parallel bzip2 implementation
Answer by Idris Rhodes
Threaded::extend — Runtime Manipulation,Threaded::isTerminated — State Detection,Thread::isJoined — State Detection,Thread::isStarted — State Detection
Here are some notes regarding PHP pThreads v3 that I have gathered:-namespace: It does not understand namespaces. -globals: It won't serialize GLOBALS at all! And will not register new ones.-classes: It registers new classes okay.-functions: Will not register ANY functions - they must all be in static classes. It does understand PHP native functions. -consts: previous constants will transfer over. Don't make any new ones thou!-pThreads only work in CLI - of course!-If a thread crashes it automatically gets recreated.-In order to 'force kill' any thread the parent must be killed. Or wait until all other tasks queued are complete and then terminate.-Anything registered in a pThread does not seem to join the main thread . which is good!-pThreads seem to be very powerful on multi-core environments, just need to be careful on system resources. it can and will lock up a system if mis-configured.-Finally, finding help for PHP pThreads is slim to none. especially v3!Good luck!
Answer by Michael Rosario
The first thing we need to do is compile our own version of PHP that will be able to support the installation of the pthreads extension.,You have completed the first step of the tutorial which is to build your own version of PHP that will support installing pthreads. Now it is time to actually add the extension.,Now add the pthreads extension to the php ini file ( and opcache whilst we’re in there).,PHP can support multithreading for compute intensive workloads. The tutorial below is divided into two parts. The first part will recompile PHP 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.
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
Install pear for installing pthreads later.
sudo apt-get install php7.0-dev php-pear -y
The previous command will have installed PHP, so remove it
sudo apt-get remove php-cli -y
Go to your home directory. We will perform all of our work in here.
Go to the releases page and grab the .tar.gz link for the latest release for the version of PHP you want. By the time you read this tutorial, these versions are likely to be out of date but serve as a working example.
wget https://github.com/php/php-src/archive/php-7.0.27.tar.gz
Extract the source code and rename it to php-src so the rest of the steps in this tutorial work no matter what version you are running.
tar --extract --gzip --file php-* rm php-*.tar.gz mv php-src-* php-src
tar --extract --gzip --file php-* rm php-*.tar.gz mv php-src-* php-src
cd $HOME/php-src ./buildconf --force
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
Update the symlink for php to point to our custom build.
sudo rm /usr/bin/php sudo ln -s /etc/php7/bin/php /usr/bin/php
You can check your version by running:
You can also check that you have ZTS support which is required for pthreads:
php -a echo PHP_ZTS; // this should output a 1 and not a 0
Use PECL to install the pthreads extesion
sudo pecl install pthreads
At the end of the previous step, the console will have told you that you need to add extension=pthreads.so to your php.ini file. However, we don’t have one yet for our custom built PHP so lets create it.
sudo mkdir -p /etc/php7/cli/ sudo cp /usr/lib/php/7.0/php.ini-production.cli /etc/php7/cli/php.ini
Now add the pthreads extension to the php ini file ( and opcache whilst we’re in there).
echo "extension=pthreads.so" | sudo tee -a /etc/php7/cli/php.ini echo "zend_extension=opcache.so" | sudo tee -a /etc/php7/cli/php.ini
You can check which version of PHP you have installed by running:
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
value = $i; > public function run() < $s=0; for ($i=0; $iecho "Task: 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();
Answer by Lainey Drake
This paper introduces the installation and configuration of PHP pthreads V3 on centos7 platform. The details are as follows:,Restart the PHP service to see if the pthreads extension is installed,(note that there is a hole in it. Do not configure pthreads extension in php.ini, otherwise PHP FPM cannot be started, because pthreads V3 version can only run in CLI),2. Run phpize to generate configure. If path is not configured, you can mark the absolute path.
http://pecl.php.net/package/pthreads
Answer by Sariah Erickson
#install imagick sudo apt-get install php-imagick #check if installed php -m | grep imagick #restart your web server sudo service apache2 restart or sudo service nginx restart