>  기사  >  백엔드 개발  >  PHP는 다중 스레드 동시성을 어떻게 구현합니까?

PHP는 다중 스레드 동시성을 어떻게 구현합니까?

不言
不言원래의
2018-07-20 16:55:3515464검색

PHP는 기본적으로 멀티스레딩을 지원하지 않습니다. 멀티스레딩을 사용하려면 pthread 확장을 설치하려면 --enable-maintainer-zts 매개변수를 사용하여 PHP를 다시 컴파일해야 합니다. 이 매개변수는 PHP를 안전한 방식으로 컴파일할 때 스레드 사용을 지정합니다.

<?php 
if(function_exists(&#39;date_default_timezone_set&#39;)) { 
date_default_timezone_set(&#39;PRC&#39;); 
} 
function a() { 
$time = time(); sleep(3); 
$fp = fopen(&#39;result_a&#39;.$time.&#39;.log&#39;, &#39;w&#39;); 
fputs($fp, &#39;Set in &#39; . Date(&#39;h:i:s&#39;, time()) . (double)microtime() . "rn"); 
fclose($fp); 
} 
function b() { 
$time = time(); 
sleep(3); 
$fp = fopen(&#39;result_b&#39;.$time.&#39;.log&#39;, &#39;w&#39;); 
fputs($fp, &#39;Set in &#39; . Date(&#39;h:i:s&#39;, time()) . (double)microtime() . "rn"); 
fclose($fp); 
} 
if(!isset($_GET[&#39;act&#39;])) $_GET[&#39;act&#39;] = &#39;a&#39;; 
if($_GET[&#39;act&#39;] == &#39;a&#39;) { 
a(); 
} 
else if($_GET[&#39;act&#39;] == &#39;b&#39;) b(); 
?>

위 코드는 로컬에서 파일을 작성합니다.

PHP 멀티 스레드 읽기 및 쓰기 파일:

localhost/a.php를 방문하여 가능한 한 빨리 두 개의 브라우저 탭을 동시에 열면 다음을 찾을 수 있습니다. 두 파일 생성 시간의 차이는 3초입니다

하지만 localhost/a.php?act=b를 방문하고 또 다른 방문/a.php?act=a를 방문하면 생성이 두 파일의 시간은 거의 동일합니다.

아파치의 경우 동일한 URL은 스레드(또는 프로세스)를 의미하지만 URL이 다르면 동시 실행이 가능하다는 의미입니다.

php 내부에 다운로드 작업이 있는 경우

function runThread() { 
down("http://localhost/test/a.php?act=a"); 
} 
if($_GET[&#39;act&#39;] == &#39;run&#39;) { 
echo &#39;start:&#39;; 
runThread(); 
echo &#39; End&#39;; 
}

http://localhost/test/a.php?act=run

http: / /localhost/test/a.php?act=run&s=2

기본 액세스 URL이 다른 한 다른 프로세스로 간주되므로 동시성을 의미합니다. 파일생성시간은 3초도 안걸립니다

로컬 리눅스 서버가 있는 친구도 리눅스를 사용해 동시성 시뮬레이션 가능

<?php for ($i=0;$i<10;$i++) { echo $i; sleep(5); } ?>

위 내용을 test.php로 저장한 후 작성 SHELL 코드

#!/bin/bash
for i in 1 2 3 4 5 6 7 8 9 10
do
php -q test.php &
done
Fixed a bug where :doc:`Image Manipulation Library <libraries/image_lib>` didn&#39;t escape image source paths passed to ImageMagick as shell arguments.
Fixed a bug (#861) - :doc:`Database Forge <database/forge>` method create_table() incorrectly accepts field width constraints for mssql/SQLSRV integer-type columns.
Fixed a bug (#4562) - :doc:`Cache Library <libraries/caching>` didn&#39;t check if Memcached::quit() is available before calling it.
Fixed a bug (#4563) - :doc:`Input Library <libraries/input>` method request_headers() ignores $xss_clean parameter value after first call.
Fixed a bug (#4605) - :doc:`Config Library <libraries/config>` method site_url() stripped trailing slashes from relative URIs passed to it.
Fixed a bug (#4613) - :doc:`Email Library <libraries/config>` failed to send multiple emails via SMTP due to "already authenticated" errors when keep-alive is enabled.
Fixed a bug (#4633) - :doc:`Form Validation Library <libraries/form_validation>` ignored multiple "callback" rules for empty, non-required fields.
Fixed a bug (#4637) - :doc:`Database <database/index>` method error() returned FALSE with the &#39;oci8&#39; driver if there was no error.
Fixed a bug (#4647) - :doc:`Query Builder <database/query_builder>` method count_all_results() doesn&#39;t take into account GROUP BY clauses while deciding whether to do a subquery or not.
Fixed a bug where

관련 권장사항:

php는 멀티스레딩, PHP 멀티스레딩은

위 내용은 PHP는 다중 스레드 동시성을 어떻게 구현합니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.