views:

862

answers:

4

I need to create a script in PHP language which performs permutation of numbers. But PHP has an execution time limit set to 60 seconds. How can I run the script so that if you need to run more than 60 sesunde, not been interrupted by the server. I know I can change the maximum execution time limit in php, but I want to hear another version that does not require to know in advance the execution time of a script.

A friend suggested me to sign in and log out frequently from the server, but I have no idea how to do this.

Any advice is welcome. An example code would be useful.

Thanks.

First I need to enter a number, lets say 25. After this the script is launch and it need to do the following: for every number <= than 25 it will create a file with the numbers generated at the current stage; for the next number it will open the previuos created file, and will create another file base on the lines of the opened file and so on. Because this take to long, I need to avoid the script beeing intrerupted by the server.

A: 
set_time_limit(0)
Funky Dude
You do realize he specifically said he did NOT want this?
Chacha102
Although this is exaclty what the question asks for, I think it would be better to for example use set_time_limit(20) in every iteration of a loop.
svens
@svens While the OP specifically says he doesn't want to change the time limit, I agree that it is probably going to be the best way to do it.
Chacha102
wooooooooooops. tl;dr my bad
Funky Dude
A: 

You could try to put the calls you want to make in a queue, which you serialize to a file (or memory cache?) when an operation is done. Then you could use a CRON-daemon to execute this queue every sixty seconds, so it continues to do the work, and finishes the task.

The drawbacks of this approach are problems with adding to the queue, with file locking and the such, and if you need the results immediately, this can prove troublesome. If you are adding stuff to a Db, it might work out. Also, it is not very efficient.

The Guy Of Doom
A: 

Use set_time_limit(0) but you have to disable the safe_mode: http://php.net/manual/en/function.set-time-limit.php I suggest to use a fixed time (set_time_limit(300)) because if there is a problem in the script (endless loops or memory leaks) this can not be a source of problems.

The web server, like Apache, have also a maximum time limit of 300 seconds, so you have to change it. If you want to do a Comet application, it may be better to chose another web server than Apache that can have long requests times.

If you need a long execution time for a heavy algorithm, you can also implement a parallel processing: http://www.google.com/#q=php+parallel+processing Or store the input data and computer with another external script with a cron or whatever else.

Davmuz
Please, specify what you have to do, the responses will may be more accurate.
Davmuz
+1  A: 

@emanuel:

I guess when your friend told you "A friend suggested me to sign in and log out frequently from the server, but I have no idea how to do this.", he/she must have meant "Split your script computation into x pieces of work and run it separately"

For example with this script you can execute it 150 times to achieve a 150! (factorising) and show the result:

// script name: calc.php

<?php

 session_start();

 if(!isset($_SESSION['times'])){

    $_SESSION['times'] = 1;

    $_SESSION['result']  = 0;

 }elseif($_SESSION['times'] < 150){

    $_SESSION['times']++;

    $_SESSION['result'] = $_SESSION['result'] * $_SESSION['times'];

    header('Location: calc.php');

 }elseif($_SESSION['times'] == 150){

    echo "The Result is: " . $_SESSION['result'];

    die();

 }

?>

BTW (@Davmuz), you can only use set_time_limit() function on Apache servers, it's not a valid function on Microsoft IIS servers.

turik
+1 Thats essentially the client-side implementation of my answer: put it into bits and execute them one by one. However, in this solution, if the browser is closed, the process ends.
The Guy Of Doom