Home > php教程 > PHP开发 > body text

How to implement multi-threading with PHP+shell

高洛峰
Release: 2016-12-21 13:28:24
Original
1198 people have browsed it

The example in this article describes the method of implementing multi-threading using PHP+shell. Share it with everyone for your reference. The details are as follows:

Here is how to implement multi-threading with the help of shell scripts.

First write a simple php code. In order to make the script execution time longer and see the effect more easily, sleep for a while, haha! First look at the code of test.php:

PHP code:

<?php
for ($i=0;$i<10;$i++) {
  echo $i;
  sleep(10);
}
?>
Copy after login

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

Did you notice that there is an & symbol in the line that requests the PHP code? This is the key, no If added, multi-threading cannot be performed. & means that the service is pushed to the background for execution. Therefore, in each loop of the shell, it is not necessary to wait for all the PHP code to be executed before requesting the next file, but at the same time, so that Multi-threading has been achieved. Run the shell below to see the effect. Here you will see 10 test.php processes running again, and then use the Linux timer to request this shell regularly to process some tasks that require multi-threading, for example, Very easy to use when downloading in batches!

I hope this article will be helpful to everyone’s PHP programming design.

For more articles related to how PHP+shell implements multi-threading, 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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!