Php asynchronous script execution code

Source: Internet
Author: User
Asynchronous execution is to suspend a script for executing specific operations in the php script in the background. after the main script exits, the suspended script can continue to be executed, for example, to execute some time-consuming operations or operations that can be executed in parallel, php can be used for asynchronous execution, communication between the main script and sub-script... asynchronous execution is to suspend a script for executing specific operations in the php script in the background. after the main script exits, the suspended script can continue to be executed, for example, to execute some time-consuming operations or operations that can be executed in parallel, php can be used for asynchronous execution. communication between the main script and sub-script can be performed using external files or memcached, the principle is to execute an external command through exec or system. Note that this article is intended for the Linux environment.

In Linux, you can add the "&" symbol at the end of the command to hold a script in the background for execution. sometimes this is not enough. you need to use the nohup command, about nohup, anyone who has played Linux should know that if you want to run a program in the background, you only need to add an & symbol at the end of the command, but this method is not very safe, some programs stop when you log out of the terminal. how can you make a program run in the background forever? the answer is to use the nohub command in the format:

Nohup command for executing the program &

If the program has output, it will try to write the output to the nohup in the current folder where the above command is executed. in the out file, when the write fails, it will write the nohup under the $ HOME directory of the current user. out.

The operations performed in the CLI environment are not the same as those in the Web environment. for the CLI environment, use nohup and & here, and specify the output. if you do not want to output the result, you can direct the output to/dev/null. now let's do a test. suppose there is main in a directory. php, sub1.php, and sub2.php. sub1 and sub2 both pause the sleep function for a period of time. the code is as follows:

//main.php 
 ./tmp.log  &';     exec($cmd);     $cmd = 'nohup php ./sub1.php >/dev/null  &';     exec($cmd);   //sub1.php sub2.php 
 

Main. php is used as the main script to execute php main. php, you can see main. the php script will soon be executed and exited. you can use the ps aux | grep sub Command to search for the process. you should be able to see the two sub-scripts in the background, indicating that the sub-scripts are successfully suspended.

In the Web environment, the execution of php scripts is handled by cgi processes enabled by the Web server. as long as the script does not exit, it will always occupy the cgi process, when all the started cgi processes are occupied, they cannot process new requests. Therefore, you can use asynchronous methods for scripts that may take a lot of time, the method for starting a script is similar to CLI. you must use & and specify the output (to be directed to/dev/null), but you cannot use nohup. for example, the following code:

 /dev/null  &';     exec($cmd);     $cmd = 'php PATH_TO_SUB1/sub2.php >/dev/null  &';     exec($cmd);  

When you access the script file in the browser, you can see that the response is complete in the browser, and you can also view the sub1 and sub2 scripts in the background using the ps command.

Note: In the above example, if the php command is not in the PATH, you need to specify the full PATH of the command. we recommend that you use the full PATH, especially on the Web.

Permanent link:

Reprint at will! Include the article address.

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.