PHP 확장 멀티스레딩

墨辰丷
풀어 주다: 2023-03-25 21:38:02
원래의
2449명이 탐색했습니다.

이 글은 주로 PHP 멀티스레딩에 대해 소개하고 있으니 관심 있는 분들이 참고하시면 좋을 것 같습니다.

PHP 5.3 이상에서는 pthreads PHP 확장을 사용하여 PHP가 진정한 멀티스레딩을 지원하도록 할 수 있습니다.
멀티스레딩은 반복적인 순환 작업을 처리할 때 프로그램 실행 시간을 크게 단축할 수 있습니다.
PHP 확장 프로그램 다운로드: https://github.com/krakjoe/pthreads
PHP 매뉴얼 문서: http://php.net/manual/zh/book.pthreads.php
1. 확장 프로그램 컴파일 및 설치(Linux), 편집 –enable-maintainer-zts 매개변수가 필요합니다:

'./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
로그인 후 복사
makemake install
로그인 후 복사
vi /usr/local/webserver/php/etc/php.ini
로그인 후 복사

추가

extension = "pthreads.so"
로그인 후 복사

2. Baidu 검색 페이지의 PHP 코드 예제를 캡처하기 위한 PHP 멀티스레딩 및 For 루프 제공:

<?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);
        }
    }
}
로그인 후 복사
<?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";
?>
로그인 후 복사

관련 권장 사항:

상세 H5+WebWorkers 멀티 스레드 개발에 대한 설명 및 사용

PHP 멀티 스레드 시뮬레이션을 통해 깜짝 세일 및 주문 확보

PHP를 통해 멀티 프로세스 및 멀티 스레드 달성

위 내용은 PHP 확장 멀티스레딩의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!