Home > Backend Development > PHP Tutorial > PHP extension multi-threading

PHP extension multi-threading

墨辰丷
Release: 2023-03-25 21:38:02
Original
2516 people have browsed it

This article mainly introduces PHP multi-threading. Interested friends can refer to it. I hope it will be helpful to everyone.

PHP 5.3 and above, using the pthreads PHP extension, can make PHP truly support multi-threading.
Multiple threads can greatly shorten program execution time when processing repetitive cyclic tasks.
PHP extension download: https://github.com/krakjoe/pthreads
PHP manual document: http://php.net/manual/zh/book.pthreads.php
1. Extension compilation Install (Linux), edit the parameter –enable-maintainer-zts is a required option:

'./configure'
'--prefix=/usr/local/webserver/php'
'--with-config-file-path=/usr/local/webserver/php/etc'
'--with-apxs2=/usr/local/webserver/httpd/bin/apxs'
'--with-libxml-dir=/usr/local/webserver/libxml2'
'--with-mysql=/usr/local/webserver/mysql'
'--with-mysqli=/usr/local/webserver/mysql/bin/mysql_config'
'--enable-soap' '--enable-sockets' '--with-curl' --enable-maintainer-ztsmake clean
make
make install
unzip pthreads-master.zip
cd pthreads-master
/usr/local/webserver/php/bin/phpize
./configure --with-php-config=/usr/local/webserver/php/bin/php-config
Copy after login
makemake install
Copy after login
vi /usr/local/webserver/php/etc/php.ini
Copy after login

Add

extension = "pthreads.so"
Copy after login

2. Give a PHP multi-threading and For loop to capture the Baidu search page PHP code example:

<?php
class test_thread_run extends Thread{
    public $url;    public $data;    public function __construct($url)
    {
        $this->url = $url;
    }    public function run()
    {
        if(($url = $this->url))
        {        $this->data = model_http_curl_get($url);
        }
    }
}
Copy after login
<?php
function model_thread_result_get($urls_array){
    foreach ($urls_array as $key => $value)
    {        $thread_array[$key] = new test_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(10);
        }        
        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 : &#39;Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2)&#39;;    $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 < 100; $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 "多线程:".($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循环:".($e-$t)."\n";
?>
Copy after login

Related recommendations:

H5 WebWorkers multi-thread development and use detailed explanation

PHP multi-thread simulation to achieve flash sales Single

PHP implements multi-process and multi-thread

The above is the detailed content of PHP extension multi-threading. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template