Home >Backend Development >PHP Tutorial >Simple example of multi-threading in php and shell
Next, the shell comes into play, calling the php file: sleep.php, the code is as follows:
Note: The key point of the above code is that you need to add an & symbol at the end of the line that requests the PHP code, otherwise multi-threading cannot be performed. & means that 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, that is, multi-threading is achieved. When you run the shell, you will see 10 test.php processes running. You can consider combining it with the Linux timer crontab to request the shell script regularly to handle some multi-threaded tasks, such as batch uploads or downloads. Okay, that’s it for the example of combining php and shell to achieve multi-threading. I hope it will be helpful to everyone. |