Well I figured I could either split this up into multiple seperate posts, or just make it one big post.
To avoid extra emailings for subscribers, I chose the latter option 🙂
So feel free to skip a question if you have no idea. 1 answer or idea is better than none 🙂
Now, the questions in nicely subtitled format:
Multi-threading fopen
Here's my problem.
Upon execution my script grabs a page from a remote internet site, parses the data, then writes it to a file (it can send to database, but this was just easier at the moment, so it can be changed). It then grabs another page, and does the same with that.
The problem is, the target site is slower than the server I use. ALOT slower.
It takes, on average, 6 seconds per query to get the information. My script must query over 1200 times to complete 1/4 of it's work.
The site changes the information every 30 minutes, so I need to grab the data in under 30 minutes.
Is there any way to use multiple fopen()s in the same script, at the same time, to accomplish what I want to do? (the target server seems to restrict used bandwidth per query, as hundreds of people use the site at the same time without extended delays)
I "could" just split the job into multiple parts and then have each script execute it's own small block of data, and have them all go at the same time.
That would work, but it is definately not wanted. It feels like a hackjob of a work around, and I'm not getting paid to do this, so I might as well learn something new in the process 🙂
Time-delay in a script
For various reasons, I often come upon a problem that could just be solved by waiting a given amount of time before continuing.
A script keeps looking to see if a given change has occurred every 10 seconds, and stops looking after 60 seconds (to avoid endless loops), for instance.
Is there absolutely ANY way to delay a script for a given amount of time , short of forcing a script to use some sort of insanely complicated mathematic equation or some sort of fopen to a slow/non-existant source (those slowing the process down by however long it takes the server to decide the connection failed)?
I've considered weird loops that do very odd things to try and delay the execution, dhtml refreshes on zombie-slave terminals, timed redirections to form submission urls, and all sorts of twisted techniques.
My hope is there is some form of PHP function to handle this that I'm simply not aware of. Or for that matter, a function in any language. It would seriously make my life easier...and happier 😉
crosses fingers
Begin writing/reading at a given point
If I had a file with an alphabetical only word (A-Z), followed by numbers (data for the word that comes before it), then followed by another word, is it possible to start fread at a given word, and/or stop it at a given point (let's say, the next word)?
Or do I just need to fread all the data into a variable and just do a regex on it?
On a similar though more important note, fwrite. How can I begin writing at a given point in a file?
For instance if I wanted to write a string into a text file after the word "hello", without deleting anything (just insert), how would I do that?
I can't find any mention of it in the fwrite documentation, and the question has been bothering me for ahwile. I get the feeling the answer is staring me right in the face (read: in the manual), but it's evaded me as of yet (the answer, not the manual).
The only idea I have had however, is to use some sort of regex to get to a certain point in the file, and then use some sort of ereg_replace to add the data.
In other words, I don't have much of a clue how to do it.
And on a last note:
Daemons
Is there a way to activate a function at a given time of day, or every so many units of time?
Other than the UNIX program that does execution upon timing (ctab,tabtime, something like that...can't recall atm), that is. Prefereably a solution within PHP, but I'm doubting it.
Just one of my pipe-dreams, which I get the feeling will get resigned to a "maybe someday" hope...or some brand of useful app (one never knows).
end;
Heh, well that's all the questions I've had (that I can remember) since even hearing about PHP, that I haven't already had answered.
Any help on any of these questions would be much appreciated.
And thanks in advance! (though I'm sure I'll end up saying thanks multiple-more times after helping)