Implementing multi-threading in PHP has always been a topic of concern to developers. With the continuous development of Internet applications, the need to handle a large number of concurrent requests is becoming more and more urgent. This article will introduce how to implement multi-threading in PHP, explore the advantages and applicable scenarios of multi-threading, and help developers better cope with complex concurrency requirements. Let's take a deeper look at the application of multithreading in PHP!
php
ParallelConcurrency extension to implement multi-threading
Using the Parallel
parallel concurrency extension, we can implement multi-threading in PHP.
This extension provides an explanation thread parallel\Runtime
. We can create a thread by creating an object from the parallel\Runtime()
class.
This class provides a method run()
, which schedules tasks to run in parallel. We can pass Closure
as a parameter to the run
method.
This parameter is generally called task
. We can also specify an array as the second parameter of the method. The contents of the array are passed to the task.
There are some requirements before downloading the Parallel
Parallel Concurrency extension. PHP version should be 8.0 and Zend Thread Safe (ZTS) should be enabled.
<pthread.h>
Headers are another requirement. We can download the extension from pecl
as shown below.
<code> <code class="language-php hljs" data-lang="php"><span style="display:flex;"><span>pecl install parallel </span></span></code></code>
We can use the for
loop to test the parallel execution of the program.
For example, we can run a loop inside the run()
method and run another loop outside the method. In this case, code execution will be parallel.
For example, create an object of class parallel\Runtime
and then use that object to call the run()
method. In the run()
method, write an anonymous function.
First, write a
loop to print the
symbol 50 times inside the function. Next, outside of the run()
method, write another for
loop that prints the -
symbols 50 times.
Since the loop inside the
method runs in a separate thread, the loop outside the run()
method will execute simultaneously. As a result, -
and
symbols are printed simultaneously, as shown in the output section below.
Therefore, we can use parallel concurrency extensions to implement multithreading in PHP.
Sample code:
<code> <code class="language-php hljs" data-lang="php"><span style="display:flex;"><span><span style="color:#19177c">$rt</span> <span style="color:#666">=</span> <span style="color:#008000;font-weight:bold">new</span> \parallel\Runtime(); </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span><span style="color:#19177c">$rt</span><span style="color:#666">-></span><span style="color:#7d9029">run</span>(<span style="color:#008000;font-weight:bold">funct<strong class="keylink">io</strong>n</span>(){ </span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">for</span> (<span style="color:#19177c">$i</span> <span style="color:#666">=</span> <span style="color:#666">0</span>; <span style="color:#19177c">$i</span> <span style="color:#666"><</span> <span style="color:#666">50</span>; <span style="color:#19177c">$i</span><span style="color:#666">++</span>) </span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">echo</span> <span style="color:#ba2121">"+"</span>; </span></span><span style="display:flex;"><span>}); </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">for</span> (<span style="color:#19177c">$i</span> <span style="color:#666">=</span> <span style="color:#666">0</span>; <span style="color:#19177c">$i</span> <span style="color:#666"><</span> <span style="color:#666">50</span>; <span style="color:#19177c">$i</span><span style="color:#666">++</span>) { </span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">echo</span> <span style="color:#ba2121">"-"</span>; </span></span><span style="display:flex;"><span>} </span></span></code></code>
Output:
<code> <code class="language-text hljs" data-lang="text"><span style="display:flex;"><span>-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ </span></span></code></code>
Use the
We can open parallel processes in PHP using the function.
This function forks the process to achieve parallel processing. Processes do not share resources.
In this way, we can implement multi-threading in PHP.
popen() The function creates a pipe to the forked process.
We can iterate over the
function and create multiple processes to achieve multi-threading. The popen()
function takes command
as the first parameter and mode
as the second parameter.
The pattern can be
or w
.
For example, create a
loop that loops five times. Inside the loop, create another for
loop that loops five times.
Within the subloop, create an array
to store the popen()
function. Set the PHP file message.php
and r
mode as the first and second parameters.
Next, create another subloop and close the
using the pclose()
function.
Here, five processes are executed in parallel in the first sub-loop. The process is terminated in the second subloop using the
function.
This is how we implement multithreading using the
function in PHP.
Sample code:
<code> <code class="language-php hljs" data-lang="php"><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">for</span> (<span style="color:#19177c">$i</span><span style="color:#666">=</span><span style="color:#666">0</span>; <span style="color:#19177c">$i</span><span style="color:#666"><</span><span style="color:#666">5</span>; <span style="color:#19177c">$i</span><span style="color:#666">++</span>) { </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">for</span> (<span style="color:#19177c">$j</span><span style="color:#666">=</span><span style="color:#666">0</span>; <span style="color:#19177c">$j</span><span style="color:#666"><</span><span style="color:#666">5</span>; <span style="color:#19177c">$j</span><span style="color:#666">++</span>) { </span></span><span style="display:flex;"><span> <span style="color:#19177c">$process</span>[<span style="color:#19177c">$j</span>] <span style="color:#666">=</span> popen(<span style="color:#ba2121">'message.php'</span>, <span style="color:#ba2121">'r'</span>); </span></span><span style="display:flex;"><span> } </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">for</span> (<span style="color:#19177c">$j</span><span style="color:#666">=</span><span style="color:#666">0</span>; <span style="color:#19177c">$j</span><span style="color:#666"><</span><span style="color:#666">5</span>; <span style="color:#666">++</span><span style="color:#19177c">$j</span>) { </span></span><span style="display:flex;"><span> pclose(<span style="color:#19177c">$process</span>[<span style="color:#19177c">$j</span>]); </span></span><span style="display:flex;"><span> } </span></span><span style="display:flex;"><span>} </span></span></code></code>
The above is the detailed content of Implementing multithreading in PHP. For more information, please follow other related articles on the PHP Chinese website!