Windows install PHP Real multi-threaded extension pthreads tutorial

Source: Internet
Author: User

Extension Address: http://docs.php.net/manual/zh/book.pthreads.php

Precautions
php5.3 or more, and is a thread-safe version. The compilers used by Apache and PHP must be consistent.
Thread-safe version is phpinfo () to view thread safety as enabled.
You can see the compiler that is used by phpinfo () to view the compiler item. my: MSVC9 (Visual C + + 2008).


I use the environment
32-bit Windows XP Sp3,wampserver2.2d (PHP5.3.10-VC9 + apache2.2.21-vc9).


First, download pthreads extension
: Http://windows.php.net/downloads/pecl/releases/pthreads
According to my environment, I downloaded the pthreads-2.0.8-5.3-ts-vc9-x86.
2.0.8 represents the version of Pthreads.
5.3 Represents the version of PHP.
TS indicates that PHP wants a thread-safe version.
Vc9 means that PHP is compiled by the Visual C + + 2008 compiler.
The x86 represents a 32-bit


Second, install pthreads extension
Copy the Php_pthreads.dll to the directory bin\php\ext\ below. (I path D:\wamp\bin\php\php5.3.10\ext)
Copy the PthreadVC2.dll to the directory bin\php\ below. (I path D:\wamp\bin\php\php5.3.10)
Copy the PthreadVC2.dll to the directory C:\windows\system32 below.
Open php config file php.ini. Add Extension=php_pthreads.dll to the back.
Tips! The Windows system needs to include the path of the PthreadVC2.dll into the PATH environment variable. My Computer---> Right---> Properties---> Advanced---> Environment variables---> System variables---> Find---> Edit---> Named path At the end of the variable value, add the full path to the PthreadVC2.dll (my C:\WINDOWS\system32\pthreadVC2.dll).


Third, test pthreads extension

classAsyncOperationextends\thread { Public function__construct ($arg){        $this->arg =$arg; }     Public functionrun () {if($this-Arg) {            printf("Hello%s\n",$this-Arg); }    } }$thread=NewAsyncOperation ("World"); if($thread-start ())$thread-Join(); ?>

Running the above code appears Hello world, indicating that the pthreads extension was successfully installed!

Attach a Thinkphp3.2.2 simple example

<?php namespace Home\controller;classTestextends\thread { Public $url;  Public $result;  Public function__construct ($url) {        $this->url =$url; }         Public functionrun () {if($this-URL) {            $this->result = Model_http_curl_get ($this-URL); }    } } functionModel_http_curl_get ($url) {    $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, ' mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2) '); $result= Curl_exec ($curl); Curl_close ($curl); return $result; }  for($i= 0;$i< 10;$i++) {    $urls[] = ' http://www.baidu.com/s?wd= '.Rand(10000, 20000); } /*Multithreading Speed Test*/$t=Microtime(true); foreach($urls  as $key=$url) {    $workers[$key] =NewTest$url); $workers[$key]->start ();} foreach($workers  as $key=$worker) {     while($workers[$key]->isrunning ()) {        Usleep(100); }    if($workers[$key]->Join()) {Dump ($workers[$key]->result); } }$e=Microtime(true);Echo"Multithreaded Time:". ($e-$t)." Seconds <br> "; /*single-thread speed test*/$t=Microtime(true); foreach($urls  as $key=$url) {Dump (Model_http_curl_get ($url)); }$e=Microtime(true);Echo"For loop time:". ($e-$t)." Seconds <br> ";

The test results are as follows:
Multithreading time: 2.83717107772827 seconds
For loop time: 10.9415860176086 seconds

Source: http://www.thinkphp.cn/topic/22676.html

Windows install PHP Real multi-threaded extension pthreads tutorial

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.