views:

223

answers:

2

Hi there, im having some real problems with the lag produced by using fgets to grab the server's response to some batch database calls im making.

Im sending through a batch of say, 10,000 calls and ive tracked the lag down to fgets causing the hold up in the speed of my application as the response for each call needs to be grabbed.

I have found this thread http://bugs.php.net/bug.php?id=32806 which explains the problem quite well, but hes reading a file, not a server response so fread could be a bit tricky as i could get part of the next line, and extra stuff which i dont want.

So my question is, what is the best/fastest way to read the response from the server as an alternative to fgets?

Thanks

A: 

Not really enough information here.

Presumably you mean you are running some PHP somewhere which calls fgets to read in data from something else - but what is the something else? You hint that its not a file - so what is it? A local program? a pipe? a network socket? a web page? ... something else?

Can you read from it faster using a different tool? What have you tried? What operating system are you running on? Do you have shell access to run netcat or similar?

Also you are talking about lag while the "bug" you refer to is primarily addressing bandwidth.

Without knowing a lot more about the problem its impossible to suggest a solution.

C.

symcbean
Sure, good points - im using a PHP framework (Predis) to interact with a Redis database. It seems that when i send a batch of commands via a socket connection from PHP to Redis, the fgets which is used to read the Redis server's response is causing the lag. Basically the Predis framework uses fgets to read responses from the server and that's where all the time is being spent - in sending and receiving data over the wire.
Peter John
So the next question is how do you know that the problem is at the PHP end? Have you tested it with another client? Have you tried running the socket as non-blocking?
symcbean
And are you sure that the delay is in PHP, and not on the database side.
Joel L
+1  A: 

file_get_contents (or stream_get_contents if you have a stream) should be the fastest way to read a server's response. Well, its the fastest way to retrieve data, but often it is the most wasteful way when looking at memory usage, since it reads all of the file at once into memory while fgets does not need to keep more than one line in memory.

You use fread as well, which is faster than fgets and which reads the file in chunks of a specific size that you can define.

If you depend on reading data linewise, you can use file() which will be slower than file_get_contents, but which gives you an array with the lines of the file.

To give you a better answer -as already mentioned above-, we need more information.

yankee