3

I'm looking for a way to run a php script multiple times from a browser. Here's the scenario:

I'm building a mySQL table from a series of large files ranging anywhere from 100 megs to 2 gigs. On average, there will be around 150,000 records in the table.

I'm doing so right now by having a javascript function that does an AJAX call to the PHP script. On success, the function sets a timeout to run itself and trigger the AJAX call to run the second hundred.

My thinking behind this was to give the function a second to close out before it runs itself again.

This isn't working so well. The whole function itself works, but performance-wise it is quite slow.

When I wasn't doing 100 records at a time and not wasn't using javascript, just PHP, I could get about 15,000 records into the table before it would time out. Right now it takes about 10 minutes for it to do the same number of records.

I know that the continuous running javascript is bleeding memory and performance like crazy and was just wondering if anyone had any ideas on how to accomplish running a PHP script over and over from a browser. Crons are not an option at this point.

Eric Strom
  • 715
  • 9
  • 20
  • The big problem may be the Javascript. – Gabriel Santos Mar 22 '12 at 18:57
  • There is huge overhead on an AJAX call, can you not start a background PHP process to run the scripts via the main page load, and then use an AJAX process to poll for updates if you need to display them. If not, the background processing would be sufficient. – Orbling Mar 22 '12 at 19:00
  • Can you clarify what is timing out? – Dark Falcon Mar 22 '12 at 19:00

4 Answers4

1

Its called (async) work/job queues, seems you need to explore Gearman

Artjom Kurapov
  • 6,115
  • 4
  • 32
  • 42
0

Couldn't you just have the PHP script itself repeat the function multiple times? If the problem is that the function sometimes fails or times out, could you could catch the exception within your script? Or do you have an unavoidable and totally fatal error that really necessitates using an external minder?

octern
  • 4,825
  • 21
  • 38
  • The run time is the biggest issue right now. It's the sort of thing where sometimes the database I'm building will be too big for the script to build in the alloted 30 seconds, which is why I need to build it 100 records at a time. – Eric Strom Mar 22 '12 at 20:05
0

I ran into a similar situation... my solution was to use an ajax queue. Essentially you feed a series of ajax calls into a queue which runs them sequentially, starting the next after the previous has returned from the server as successful.

Setting a timeout can run into a situation where the next ajax call is made before the server completed the last. This is the likely cause of your performance issue. I don't really like javascript timeouts myself just for the resource overuse alone.

Google "Ajax Queue" for code that you find useful, or I can post mine, which is jQuery.

R2-Bacca
  • 150
  • 6
-1

configure a cronjob to run your script every minute

Adriaan Nel
  • 370
  • 1
  • 11