Home>Article>Backend Development> An implementation method of PHP multi-threading—shell
Does PHP itself support multi-threading? However, we can use other methods to achieve multi-threading, such as shell services, such as web Server, how to implement multi-threading with the help of shell script?
In this article, we mainly introduce the method of implementing multi-threading in PHP+shell. You can refer to it.
Here is how to implement multi-threading with the help of shell scripts.
Write a simple php code first. In order to make the script execution time longer and to see the effect more easily, let’s look at the code of test.php first:
PHP code:
Looking at the code of the shell script, it is very simple
#!/bin/bash for i in 1 2 3 4 5 6 7 8 9 10 do /usr/bin/php -q /var/www/html/test.php & done
Did you notice that there is an & symbol in the line that requests the php code? This is the key. Without it, multi-threading cannot be performed, & means The service is pushed to the background for execution. Therefore, in each loop of the shell, you do not have to wait for all the PHP code to be executed before requesting the next file, but at the same time. This achieves multi-threading. Run the shell below to see As a result, here you will see 10 test.php processes running again, and then using the Linux timer to request this shell regularly. It is very useful when processing some tasks that require multi-threading, such as batch downloading!
Summary: Threads are a very good thing in Java. Many friends say that PHP multi-threading cannot be used in PHP. In fact, that is a wrong statement. This article introduces an implementation method of multi-threading in PHP. .
I hope this article will be helpful to everyone’s PHP programming design.
Related recommendations:
PHP multi-threading small case
Two implementation methods of multi-threading in PHP
The above is the detailed content of An implementation method of PHP multi-threading—shell. For more information, please follow other related articles on the PHP Chinese website!