Home >Backend Development >PHP Tutorial >How does php implement multi-thread concurrency?

How does php implement multi-thread concurrency?

不言
不言Original
2018-07-20 16:55:3515555browse

PHP does not support multi-threading by default. To use multi-threading, you need to install the pthread extension. To install the pthread extension, you must use the --enable-maintainer-zts parameter to recompile PHP. This parameter specifies the use of thread safety when compiling PHP. Way.

<?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(); 
?>

The above code writes a file locally.

PHP multi-threaded reading and writing files:

If you visit localhost/a.php and open two browser tabs at the same time as quickly as possible, you will find that the difference in the creation time of the two files is 3 seconds.

But if you visit localhost/a.php?act=b and another visit/a.php?act=a, you will find that the creation time of the two files is almost the same.

For apache, the same URL means a thread (or process), but different URLs mean that it can be concurrent.

If there is a download action inside 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

As long as the URL of the main access is different, it is considered to be different processes, which means concurrency. The file creation time is not 3 seconds

Friends who have a local Linux server can also use Linux to simulate concurrency

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

Save the above into test.php, and then write a SHELL code

#!/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

Related recommendations:

php implements multi-threading, php multi-threading

The above is the detailed content of How does php implement multi-thread concurrency?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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

Related articles

See more