tags:

views:

37

answers:

4

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.

A: 

If you can use system command, then it is possible.

  1. Create php scripts what do the request and write the response data to files (10 files in total)
  2. Write a php script that call system command to run those 10 php script above, then wait for all executions is done
  3. Read the response data from files
Bang Dao
A: 

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

Byron Whitlock
+2  A: 

Yes, you can use curl. See here in the manual.

You can also use non-blocking I/O.

Artefacto
A: 

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: http://stackoverflow.com/questions/124462/asynchronous-php-calls

Justin Lucas