PHP + shell implements multiple threads, and phpshell implements multiple threads

Source: Internet
Author: User

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.

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.