PHP Asynchronous Execution script

Source: Internet
Author: User
Tags php script sleep function sub command

The asynchronous execution here is to have the PHP script suspend a script in the background that performs a specific action, and after the main script exits, the pending script can continue to execute. For example, some time-consuming operations or operations that can be performed in parallel can take the form of PHP asynchronous execution. Communication between the main script and the sub-script can take the form of an external file or memcached. The principle is to execute an external command via exec or system. Note: This article describes the Linux environment.

Under Linux to let a script hanging in the background can be executed at the end of the command with a "&" symbol, sometimes this is not enough, you need to use the Nohup command, about Nohup, you can refer to http://www.netingcn.com/linux-nohup.html.

The CLI environment and the Web environment do not have the same actions. First of all, the CLI environment, the need to use the nohup and &, but also the specified output, if you do not want to output the results, you can direct the output to/dev/null. Now do a test, assuming that there are main.php, sub1.php, and sub2.php in a directory, where Sub1 and sub2 content let the sleep function pause for a while. The code is as follows:

In the above file, main.php is the main script, executing PHP main.php on the command line, you can see that the main.php script executes and exits shortly. When using PS aux | The grep sub command search process should be able to see the above two sub-scripts in the background, stating that the child script was successfully suspended.

In a Web environment, executing PHP scripts is handled by the Web server's CGI process, and as long as the script does not exit, it will always occupy the CGI process, and when all the CGI processes that are started are consumed, new requests cannot be processed. So for scripts that can be time-consuming, you can use asynchronous methods. You start a sub-script in the same way as the CLI, you must use & and specify the output (only directed to/dev/null), but you cannot use Nohup. For example:

When the script file is accessed in the browser, you can see that the response in the browser is complete, and you can see the sub1 and SUB2 scripts using the PS command to view the background.

Note In the above example, if the PHP command is not in path, you need to specify the full path to the command. It is recommended to use the full path, especially under the web.

PHP Asynchronous Execution script

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.