Home > Backend Development > PHP Tutorial > PHP multi-thread concurrency implementation method

PHP multi-thread concurrency implementation method

高洛峰
Release: 2023-03-03 19:38:02
Original
1396 people have browsed it

The example in this article describes the implementation method of multi-thread concurrency in PHP. Share it with everyone for your reference, the details are as follows:

Multi-threading in Java is a new thread thing. PHP relies on Apache and there is a multi-threading method at the bottom of Linux.

Here is how to simulate PHP concurrency if you cannot control the Apache server

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

The above code writes a file locally.

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 creation time of the two files is 3 seconds

But if you visit localhost/a.php?act=b another one Visit /a.php?act=a and you will find that the two files were created at almost the same time.

For apache, the same URL means a thread (or process), but different URLs mean concurrency.

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
Copy after login

As long as the URLs visited by the main users are 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);
}
?>
Copy after login

Save the above as 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
Copy after login

Hope What this article describes will be helpful to everyone in PHP programming.


For more articles related to PHP multi-threaded concurrency implementation methods, please pay attention to 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