PHP fastest method of reading server response
Asked Answered
A

2

6

I'm having some real problems with the lag produced by using fgets to grab the server's response to some batch database calls I'm making.

I'm sending through a batch of say, 10,000 calls and I've 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 he's 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 don't want.

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

Amberlyamberoid answered 19/4, 2010 at 20:48 Comment(3)
what you asking about? php language dont have nothing like speed processing but this language is not for.Strep
And can you show some code? What kind of database calls are these?Aplanatic
they are calls to Redis NoSQL serverAmberlyamberoid
P
1

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.

Plerre answered 27/4, 2010 at 21:35 Comment(1)
@AbiusX: file_get_contents is supposed to do exactly that. If you deserve different behaviour, plz explain.Plerre
H
0

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.

Huerta answered 19/4, 2010 at 21:37 Comment(3)
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.Amberlyamberoid
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?Huerta
And are you sure that the delay is in PHP, and not on the database side.Stomatitis

© 2022 - 2024 — McMap. All rights reserved.