Home > Article > Backend Development > Four ways of asynchronous execution in php
1. Use Ajax and img tag
Principle: Insert Ajax code or img tag into the HTML returned by the server, and the src of img is the program that needs to be executed.
Advantages: Simple implementation, the server does not need to perform any calls.
Disadvantages: During execution, the browser will always be in the loading state, so this method is not a true asynchronous call.
(Free video tutorial recommendation: php video tutorial)
2. Use popen
Use popen to execute Command, syntax:
Advantages: Fast execution
Disadvantages:
1. Can only be executed on this machine
2. You cannot pass a large number of parameters
3. Many processes will be created when the traffic is high
3. Use curl
to set the curl timeout CURLOPT_TIMEOUT to 1 (minimum is 1), so the client needs to wait 1 second.
4. Using fsockopen
fsockopen is the best. The disadvantage is that you need to splice the header part yourself.
Note: During the execution process, the client connection is disconnected or the connection times out, which may cause incomplete execution, so it is necessary to Plus.
Recommended related articles and tutorials: php tutorial
The above is the detailed content of Four ways of asynchronous execution in php. For more information, please follow other related articles on the PHP Chinese website!