PHP + shell implements multiple threads, and phpshell implements multiple threads
This example describes how to implement multithreading in PHP + shell. Share it with you for your reference. The details are as follows:
This section describes how to use shell scripts to implement multithreading.
First, write a simple php code. Here, to make it easier to execute the script for a longer time, you can sleep it! Let's take a look at the code of test. php:
PHP code:
<? Phpfor ($ I = 0; $ I <10; $ I ++) {echo $ I; sleep (10) ;}?>
The shell script code is very simple.
#! /Bin/bashfor I in 1 2 3 4 5 6 7 8 9 10do/usr/bin/php-q/var/www/html/test. php & done
Note that there is an & symbol in the line requesting php code. This is the key. If it is not added, it cannot be multithreading. & it indicates that the service is pushed to the background for execution. Therefore, in each loop of the shell, you do not have to wait until all php code is executed in the next file of the request, but at the same time. In this way, multiple threads are implemented. Run the shell below to check the effect, here you will see 10 test. the php process runs again, and then uses the linux timer to regularly request this shell to handle some tasks that require multithreading, such as batch download, which is very useful!
I hope this article will help you with php programming.