1

I'm hoping there is a simple solution here:

My code is basically

....some code.... //populates a MySQL table

include 'run_this_page.php'; //uses data from table above and takes ten minutes to run

....lots more code.... ///also uses data from same table

As the include takes ten minutes to excute, I'd like for it to be initiated and then move on immediately to the next step. Nothing in the include is required for these next steps. I can think of several non-elegant (create a txt file which will trigger a scheduled task in Windows rather than using include etc) ways to achieve this, is there a more elegant method?

*Update*

I'm being attacked (politely) by many over the structure of my script. Let me explain a bit more what the purpose is:

1 - Raw data is gathered from a 3rd party API, filtered via the script and stored in the database. Because of limitations in this API, I have to restrict the requests to small chunks and this can easily take over 20 minutes. From this data I am looking for two distinct result sets. Each one of these sets are stored in its own table. This will soon grow to a dozen result sets in a dozen tables. (I could use only one table, but it makes no difference for our discussion).

2 - Now that I have the result sets, I have two distinct scripts that go out and gather data about each. Because the target 3rd party sites are completely different I wrote the scripts in their own files and simply want them to both run now. Each can take upwards of ten minutes.

I hope this explanation helps

13
  • 4
    the real question is: why does the include take 10 minutes. Solve that problem instead. PHP scripts are supposed to get invoked, run their course, and die() again, all within 30 seconds, but really within only 2 or 3 Apr 8, 2014 at 0:36
  • A task scheduler on the server like cron in linux sounds like the best solution to call your long-running script periodically.
    – jeroen
    Apr 8, 2014 at 0:36
  • +1 for @Mike'Pomax'Kamermans why does your include take 10minutes? thats far too long if you ask me... Apr 8, 2014 at 0:37
  • if cron is out of the question, which it may be depending on the host you have, why not just move the include to the bottom of the script?
    – Kai Qing
    Apr 8, 2014 at 0:39
  • 1
    @jeroen This script is not initiated by users and will never have more than one instance running Apr 8, 2014 at 0:51

2 Answers 2

3

A simple method to load PHP asynchronously is to just use jQuery and AJAX.

<? // php code you want to run 1st ?>
<div id="content"></div>
<script>
$( "#content" ).load( "example.php" ); // php to run 3rd
</script>
<? // php code you want to run 2nd ?>

OR ...

A more robust solution, if you want to load example.php on demand, would be to run a cron job programmatically when the user clicks a 'process data' button on that page.

Then you could use AJAX to ping the server every 60 seconds until the data is updated and pull it in using the same code from above.

OR ...

php execute a background process

8
  • From the original question some code and lots more code will load and display immediately without waiting for the content of run_this_page.php.
    – Justin
    Apr 8, 2014 at 0:41
  • 1
    but the wait is 10 minutes, just bypassing it still leaves a 10 minute async wait, and the browser will happily stop waiting well before that load is done. Also PHP will kill off the script well before then on a typical setup, so this is an "answer" but it kind of addresses the wrong problem. Apr 8, 2014 at 0:42
  • 1
    You are making a lot of assumptions about his app. You may be spot on, or maybe he really does have business rules the require him to wait 10 min for run_this_page.php. You can definitely tell PHP to wait that long if you want/need to. A solution for the question he asked is certainly AJAX. Solving long load times is a different question all together and not one that he mentions.
    – Justin
    Apr 8, 2014 at 0:46
  • You should not rely on a client-side browser to do a job that seems to be 100% server-side.
    – jeroen
    Apr 8, 2014 at 0:50
  • 1
    @jeroen I should mention that this is run through the command prompt and I modified all settings so that it doesn't time-out. Apr 8, 2014 at 1:05
0

Open a socket, write to it and close the socket. Be careful, your code will be executing asynchronously:

$sock = fsockopen($url,$port);
$packet = "POST $path?$get HTTP/1.1\r\n";
$packet.= "Host: $host\r\n";
$packet.= "Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n";
$packet.= "Connection: Close\r\n\r\n";
$packet.= $post;
fwrite($sock, $packet);
fclose($sock);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.