Other requests are required in PHP development
HTTP (s) service interfaceMany people will directly use the built-in
curl_multiYou can combine the previous individual
Curl requestIt is added to a batch to execute in parallel, which is implemented in terms of time overhead
Max (interface 1, interface 2, interface 3...)It can reduce the sense of waiting for users when our business needs multi service data aggregation.
- Time optimization is more than
Max (interface 1, interface 2, interface 3...), reachable
Max (business code, interface 1, interface 2, interface 3...), so that the waiting period after the request can also be used to execute business code.
curl curl_multiFriendly use of encapsulation, object encapsulation to simplify all kinds of code
curl_*Memory and clutter of functions.
PHP8.0.0 TS x64
curl_multiCreate a batch and transfer multiple
curlHandle is added and the batch is executed
curl_multi_exec()Handle to perform the operation required in the batch.
Receive processing response, no action is required while waiting for a response
curl_multi_select()Block wait until
- If a handle receives a response.
- This function can avoid dead cycle idling exec during the waiting period. The thread should be in a suspended state and awakened by the underlying trigger.
- The core of optimization is
- Debug execution found through
curl_multi_exec()The first time is to send HTTP request message. It is speculated that the execution granularity of exec is the processing of HTTP request / response message.
- Therefore, we can first send the request, then execute our own business logic, and finally obtain the execution result when necessary. In this way, the waiting time for response can be reused.
HTTPSWill be involved many times
HTTP messageInteraction, optional optimization parameters are provided in the encapsulation as far as possible.
- Encapsulation & test code warehousegitee.com/VwenX/curl-mut
- Local use
httpServer (the code is shown in the figure below, and it can be used if you look for it on the Internet)
HTTP serviceWaiting after receiving the request is set in
300msThe response is made only after, and the actual waiting time is output in the response
- PHP requests three interfaces as a client and simulates the execution of business for 1 second at the same time
- Finally, we can see that the time consumption of the three interfaces is more than 310ms.
- If it is called as usual, the overall time consumption is
Max (a, B, c) + business=Over 1317ms. However, the overall time-consuming after our processing is only 1017ms, and the three 310 + MS requests hardly take up any time overhead!
This work adoptsCC agreement, reprint must indicate the author and the link to this article