Avoid PHP execution time limit

落爺英雄遲暮 提交于 2019-12-05 07:44:44

@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.

set_time_limit(0)

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!