PHP Refresh Scripts: Fork
January 18, 2005 8:20 AM   Subscribe

Ok, so I have a php script that updates a txt file on my server with data pulled from an rss feed on Scrobbler. I have it set to refresh the feed when a user accesses a page on my site, and the local txt file is stale. There's (obviously) a noticeable lag accessing this page when the feed is being refreshed. My question: Is there a way to call the refresh script such that it would be its own process, independent of my pagebuilding script.
posted by crumbly to Computers & Internet (6 answers total)
 


depending on some server and browser issues (see link), you could also just put the 'refresh script' at the end of the php file. build all the page output, use flush() to send the output back, and then call the refresh function.
posted by danOstuporStar at 8:43 AM on January 18, 2005


The only downside to a cron job is that it will be called regulary even if you have no-one actually asking for that information.

If you want to avoid this, then you could use fork when the cache needs updating. The child could then go off and pull the data, whilst the original thread presents a message such as "updating information, please wait". Obviously you wouldn't want it to fork if a child is already pulling the data, otherwise you'd end up with a lot of children.

Then you'd need a meta-refresh in the "please wait" page which calls the same script again, checks to see if the update is still in progress and if it is, displays the same "please wait" message.

If not, then it could display the fresh information.
posted by ralawrence at 8:49 AM on January 18, 2005


Not all hosting services provide cron.

Alternatives:

Use a free website monitoring service such as siteuptime to open a requested page on a regular basis.

Add an item to your local PC's scheduled tasks to do a wGet on the php page at regular windows. A google for wGet for windows returns this, but I don't know which of these are any good / trustworthy.

I'm assuming that you're quite technical, but if not, say and I'll provide more comprehensive instructions.
posted by seanyboy at 3:17 PM on January 18, 2005


I'd go with ralawrence, forking a process can be a little tricky first time thorugh but really useful when you have it beat.

In terms of ensuring your process isn't running more than once at any one time, set up something of a mutex.
- Get the script to create an empty file called "processing" or something before it starts doing its job.
- Make the script check to see if that file exists right before it creates though, and if it already exists, a separate thread is already at work and doesn't need to run again - kill the current thread.
- If the process has run, delete the mutex file so that another thread can run at a later time.

I probably didn't need to say all that.
Good luck.
posted by NinjaPirate at 7:22 AM on January 19, 2005


Response by poster: Thanks for info everyone. 'Fork' ended up being the magical keyword that led me to exec() (and wget), which was exactly what I was looking for. And NinjaPirate, that's exactly what I did; I had no idea it had such a cool name.
posted by crumbly at 7:41 PM on January 19, 2005


« Older Newspaper article fair use   |   Starting a Food Export Business? Newer »
This thread is closed to new comments.