Multi-threaded use of PHP

Source: Internet
Author: User
Tags install php vc9 zts

PHP 5.3 or later, using the pthreads PHP extension, you can make PHP really support multi-threading. Multithreading in the processing of repetitive cyclic tasks, can greatly shorten the program execution time.

Installation under the Liunx

Preparatory work:

1. Download Threading for PHP installation package https://github.com/krakjoe/pthreads

2.php installation Package

PHP installation must be added--enable-maintainer-zts parameters This is a security thread

Yum Install Php-devel php-pear httpd-devel
wget http://www.php.net/distributions/php-5.5.8.tar.gz
Tar zxvf php-5.5.8.tar.gz
CD php-5.5.8.tar.gz
./configure--prefix=/usr/local/php--with-config-file-path=/etc--enable-maintainer-zts
Make && make install
(make-j3 && make-j3 install), Faster Building
CP Php.ini-development/etc/php.ini
PECL Install Pthreads
Prompt error
Error:pthreads requires ZTS, please re-compile PHP with ZTS enabled
No, I added the security thread parameter, How also reported wrong ...
It seems that when I installed php-devel with yum, I installed PHP.
So
Yum Remove PHP
Recompile in the installation
PECL Install Pthreads
Tip error: ' Phpize ' failed
cd/usr/local/php/bin/
See if there are phpize
Found Phpize, stating that it was not a php-devel problem.
There is a problem with installing the extension, so PHP installation is not a problem
Download Pthreads Expansion pack
Unzip Pthreads-master.zip
CD pthreads
/usr/local/php/bin/phpize
./configure--with-php-config=/usr/local/php/bin/php-config
Make && make install
Tip Installing shared extensions:/usr/local/php/lib/php/extensions/no-debug-zts-20121212/
Indicates successful installation
Modify the php.ini file to add extension=pthreads.so
echo "extension=pthreads.so" >>/etc/php.ini
Php-v
Hint warning:php startup:unable to load Dynamic Library '/usr/lib64/php/modules/pthreads.so '-/usr/lib64/php/modules/ Pthreads.so:cannot open Shared object file:no such file or directory in Unknown on line 0
Not a very satisfactory installation
Wrong I wipe
Check if/usr/local/php/include/php/main/php_config.php has a # define ZTS 1
There's this in there. I wipe what's the matter? Continue Baidu

re-install a try

CD php-5.5.8.tar.gz

./configure--prefix=/usr/local/php--with-config-file-path=/etc--enable-maintainer-zts
Make-j3 && make-j3 Install
CP Php.ini-development/etc/php.ini

Go to Pthreads Extract directory
CD ~/soft/pthreads-master
Run the Phpize script
/usr/local/php/bin/phpize
./configure--with-php-config=/usr/local/php/bin/php-config

Make && make install
Prompt for installation success
echo "extension=pthreads.so" >>/etc/php.ini
Php-v

PHP warning:php startup:unable to load Dynamic Library '/usr/lib64/php/modules/pthreads.so '-/usr/lib64/php/modules/pt hreads.so:undefined symbol:core_globals_id in Unknown on line 0

It's the same mistake I don't know.

Use of multithreading under Windows

version of Windows
http://windows.php.net/downloads/pecl/releases/pthreads/0.1.0/
I PHP version is 5.4.17, so download php_ Pthreads-0.1.0-5.4-ts-vc9-x86.zip file package, where 0.1.0 is represented as the current pthreads version number, 5.4 is the PHP version number, TS is previously judged PHP corresponding TS, NTS version, VS9 represents the visual The Studio compiler compiler compiles, and the last x86 represents a 32-bit version. Unzip the downloaded Php_pthreads-0.1.0-5.4-ts-vc9-x86.zip package to find the PthreadVC2.dll and Php_pthreads.dll files. 1, modify the php.ini file to add extension= Php_pthreads.dll
2, put Php_pthreads.dll C:\wamp\bin\php\php5.5.12\ext below 3, will pthreadVC2.dll placed under the C:\wamp\bin\php\php5.5.12 4, Put PthreadVC2.dll under C:\Windows\System32 5, put the absolute path of the PthreadVC2.dll into the PATH environment variable 3, restart the Apache server
<?PHPclassTest_thread_run extends Thread { Public$url;  Public$data;  Publicfunction __construct ($url) {$ This->url =$url; }           Publicfunction Run () {if(($url = $ This-URL)) {                $ This->data =Model_http_curl_get ($url); }}} function Model_thread_result_get ($urls _array) {foreach($urls _array as$key =$value) {$thread _array[$key]=NewTest_thread_run ($value ["URL"]); $thread _array[$key]-start (); }          foreach($thread _array as$thread _array_key =$thread _array_value) {             while($thread _array[$thread _array_key]->isrunning ()) {Usleep (Ten); }            if($thread _array[$thread _array_key]->join ()) {$variable _data[$thread _array_key]= $thread _array[$thread _array_key]->data; }        }        return$variable _data; } function Model_http_curl_get ($url, $userAgent="") {$userAgent= $userAgent? $userAgent:'mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2)'; $curl=Curl_init ();        curl_setopt ($curl, Curlopt_url, $url); curl_setopt ($curl, Curlopt_returntransfer,1); curl_setopt ($curl, Curlopt_timeout,5);        curl_setopt ($curl, curlopt_useragent, $userAgent); $result=curl_exec ($curl);        Curl_close ($curl); return$result; }       for($i =0; $i <Ten; $i + +) {$urls _array[]= Array ("name"="Baidu","URL"="http://www.baidu.com/s?wd=". Mt_rand (10000,20000)); } $t= Microtime (true); $result=Model_thread_result_get ($urls _array); $e= Microtime (true); Echo"Multithreading:". ($e-$t)."\ n"; $t= Microtime (true); foreach($urls _array as$key =$value) {$result _new[$key]= Model_http_curl_get ($value ["URL"]); } $e= Microtime (true); Echo"For Loop:". ($e-$t)."\ n"; ?>

Multithreading: 5.1022920608521 for Loop: 20.272159099579

Multithreading takes much less time than a single thread. Windows under multi-threaded PHP Test success!!!

Multi-threaded use of PHP

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.