I have a script that makes calls to an XML api on a remote server. Currently my script sends 10 requests serially to the remote server. It fully processes each request before sending the next one. This is a huge bottleneck for my server at the moment since each API request can take up to a second each. Since most of the time is spent waiting for the remote server to respond, I'm wondering if/how I can send the requests in parallel so that all 10 requests use the same one second latency instead of ten one second latencies...
I thought about calling the script 10 times using a system command and running them in the background to effectively create 10 processes, but I'm not sure if that's the best way to do it. I figure this problem has probably been solved before.
Yes, you can use curl. See here in the manual .
You can also use non-blocking I/O .
If you can use system command, then it is possible.
PHP doesn't support threads, but you use curl_multi .
That will send your requests in parallel. This is a good solution because most of the time you are waiting on the network anyway.
If you design carefully (use a queue for urls, issue a processing callback when each one is done) you won't have to wait until the longest request is finished
What you're looking for is probably asynchronous calls. It can be done a couple of different ways in PHP depending on the version you're using. Some great information on it in this question: Asynchronous PHP calls?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.