Php+shell Method of implementing multithreading

Source: Internet
Author: User

Php+shell method of implementing multithreading

Here's how to implement multithreading with Shell scripting.

Write a simple PHP code first. This is to allow the script to run for a longer time. Easy to see effect, sleep a bit. Oh. First look at the test.php code: LS

PHP Code:

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

Look at the code of the shell script, very easy

?

12345 #!/bin/bashfori in 1 2 3 4 5 6 7 8 9 10do  /usr/bin/php-q /var/www/html/test.php &done

Notice that the line in the request for PHP code has a & symbol, this is the key, no words can not be multithreaded,& Express service push to the background to run. Therefore, in the shell every time the loop does not have to wait for the PHP code to run all finished in the request next file, but at the same time. This implements the multi-threaded, below run under the shell to see the effect, here you will see 10 test.php process to run again, and then use the Linux timer. Request this shell on a timed basis to handle some tasks that require multiple threads. For example, when downloading in bulk. Very good!

Php+shell Method of implementing multithreading

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.