Home  >  Article  >  Backend Development  >  Introduction to php asynchronous execution script

Introduction to php asynchronous execution script

不言
不言Original
2018-07-04 17:07:141985browse

This article mainly introduces the introduction of PHP asynchronous execution scripts. It has certain reference value. Now I share it with everyone. Friends in need can refer to it.

The asynchronous execution mentioned here is to let PHP scripts Suspend a script that performs specific operations in the background. After the main script exits, the suspended script can continue to execute. For example, to perform certain time-consuming operations or operations that can be performed in parallel, PHP can be executed asynchronously. The communication between the main script and the subscript can be through external files or memcached. The principle is to execute an external command through exec or system. Note: This article describes a Linux environment.

To make a script execute in the background under Linux, you can add an "&" symbol at the end of the command. Sometimes this is not enough, and you need to use the nohup command. For nohup, you can refer to http:// www.netingcn.com/linux-nohup.html.

The operations performed by the CLI environment and the Web environment are not the same. Let’s talk about the CLI environment first. You need to use nohup and & here, and also specify the output. If you don’t want to output the results, you can direct the output to /dev/null. Now let's do a test. Suppose there are main.php, sub1.php and sub2.php in a directory. The contents of sub1 and sub2 are the same and the sleep function is paused for a period of time. The code is as follows:

#main.php in the above file is used as the main script. Execute php main.php in the command line. You can see that the main.php script is executed quickly. Finish and exit. When using the ps aux | grep sub command to search for processes, you should be able to see the above two subscripts in the background, indicating that the subscripts have been successfully suspended.

In the Web environment, the execution of PHP scripts is handled by the cgi process started 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, it will Cannot process new requests. So for those scripts that may be time-consuming, you can use an asynchronous approach. The way to start a subscript is similar to the CLI. You must use & and specify the output (you have to direct it to /dev/null), but you cannot use nohup. For example:

When you access the script file in the browser, you can see that the response in the browser is completed. At the same time, use the ps command to view the background and you can see the sub1 and sub2 scripts.

Note that in the above example, if the php command is not in the PATH, you need to specify the complete path of the command. It is recommended to use the full path, especially under the Web.

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

PHP’s file and directory operations

Basics of how PHP works

The above is the detailed content of Introduction to php asynchronous execution script. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn