Reputation: 1879
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?
Upvotes: 6
Views: 2697
Reputation: 40810
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.
Upvotes: 1
Reputation: 48357
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.
Upvotes: 0