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

user2029890
  • 2,493
  • 6
  • 34
  • 65
  • 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 – Mike 'Pomax' Kamermans Apr 08 '14 at 00: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 08 '14 at 00:36
  • +1 for @Mike'Pomax'Kamermans why does your include take 10minutes? thats far too long if you ask me... – Kim Oliveros Apr 08 '14 at 00: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 08 '14 at 00:39
  • Fair questions. It is using Curl to gather data from 3rd party sites and it takes time to gather it all. – user2029890 Apr 08 '14 at 00:40
  • But if the rest of your code does not depend on its results, why would you need to include it in this script? Just run it once a day / hour / etc. automatically. – jeroen Apr 08 '14 at 00:44
  • @jeroen Because the data which is gathered in step 1 changes frequently and it needs to be as fresh as possible when it moves on to step 2. Same for the data in step 3 – user2029890 Apr 08 '14 at 00:46
  • @user2029890 don't overload your PHP script with that. Your crawls for data should be independent of your serving script. Dump the scrape into a repository (SQL, noSQL, what have you), making your script reach into that repository. At a 10 minute crawl, your data is **always** going to be stale by the time you're done crawling. – Mike 'Pomax' Kamermans Apr 08 '14 at 00:47
  • @user2029890 That may be, but a few users / visitors using your script within a 10-minute time-span, and you will be doing the same job multiple times simultaneously and the current state of your database (assuming you store the results) are a mess. – jeroen Apr 08 '14 at 00:49
  • Guys, processing huge amounts of data can take a long time. However, it's a valid concern as to why you would want to include this right in the middle of your script. – bingo Apr 08 '14 at 00:50
  • 1
    @jeroen This script is not initiated by users and will never have more than one instance running – user2029890 Apr 08 '14 at 00:51
  • Well, that at least reduced the number of problems you have by one :-) – jeroen Apr 08 '14 at 00:52
  • It seems like everyone agrees I'm doing something wrong here. I'm updating my original question to better explain what my script is designed to do – user2029890 Apr 08 '14 at 00:54

2 Answers2

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

Community
  • 1
  • 1
Justin
  • 26,443
  • 16
  • 111
  • 128
  • 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 08 '14 at 00: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. – Mike 'Pomax' Kamermans Apr 08 '14 at 00: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 08 '14 at 00:46
  • You should not rely on a client-side browser to do a job that seems to be 100% server-side. – jeroen Apr 08 '14 at 00:50
  • Please post a better answer if you know of one. He asked for a simple solution, AJAX is exactly that. – Justin Apr 08 '14 at 00:52
  • 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. – user2029890 Apr 08 '14 at 01:05
  • @user2029890 I'm kind of lost here, if you run it from the command line and you want some code to be processed right away, why don't you just put the `include` line at the end and let the script run its course? – jeroen Apr 08 '14 at 01:17
  • @jeroen Because I don't want it to wait for the other code. To simplify, assume the other code is also an include and I want both includes to run immediately after the original code is completed without waiting for the other to finish. – user2029890 Apr 08 '14 at 01:19
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);
bingo
  • 2,298
  • 1
  • 15
  • 21