How to implement Multithreading in PHP

Source: Internet
Author: User
This article mainly introduces the implementation of multi-threaded PHP method, interested in the reference of friends, I hope to be helpful to everyone.

This paper describes the Php+shell implementation of multi-threading methods, as follows:

Here's how to implement multithreading with Shell scripting.

First write a simple PHP code, here in order to make the script execution time longer, convenient to see the effect, sleep, hehe! First look at the test.php code:

PHP Code:

<?phpfor ($i =0; $i <10; $i + +) {  echo $i;  Sleep (10);}? >

Look at the code of the shell script, 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

Notice that the line in the request for PHP code has a & symbol, this is the key, not add the words is not able to multi-threaded,& service push to the background execution, so in the shell every time the loop does not have to wait for the PHP code to complete after the request of the next file, but at the same time , so that the implementation of multi-threading, under the shell to see the effect, here you will see 10 test.php process and then run, and then use the Linux timer, timed to request the shell, in the processing of a number of multi-threaded tasks, such as bulk download, very useful!

Summary: the above is the entire content of this article, I hope to be able to help you learn.

Related recommendations:

PHP tips for caching with file storage

PHP for uploading image files of the function of the detailed

PHP uses regular expressions to extract links from content

PHP recursive traversal for unlimited classification

PHP Process Control and mathematical operations

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.