PHP - Possible Obstacles When Running Long Scripts

I have a rather long loop running in a function, but it does not end all possible iterations and stops without any errors:

function my_function(){

    foreach (range(100,999) as $art_id){ 


    $current++;//see bottom flush functions...
    outputProgress($current, $art_id );//see bottom flush functions...

// do a lot of stuff on remote URL...
    // including download images , 
    // scraping HTMl etc ..

    }
}

I use some output using flush to track progress

function outputProgress($current, $total) {
    // echo "<span style='background:red;font-size:1.5em;color:#fff;'>" . round($current / $total * 100) . "% </span>";
    echo "<span style='background:red;font-size:1.5em;color:#fff;'>" . $current .'/'. $total  . "% </span>";
    myFlush();
    sleep(1);
}

and

function myFlush() {
    echo(str_repeat(' ', 256));
    if (@ob_get_contents()) {
        @ob_end_flush();
    }
    flush();
}

(do not count the percentage calculation, now it is disabled and just shows the iteration id)

I noticed that most of the time I do a loop, It just stops after 20-25 iterations. sometimes only 10.

My first suspects were time limitand max_execution time, therefore, I added:

set_time_limit(99999);
ini_set('max_execution_time', 99999);

function my_function(){

    foreach (range(410,499) as $art_id){ // done 500-600

    set_time_limit(99999);
    ini_set('max_execution_time', 99999);

    // do a lot of stuff on remote URL...
    // including download images , 
    // scraping HTMl etc ..
    }
}

As you can see, I added both of these INSIDEand OUTSIDEthe functions themselves just in case.

But that doesn’t help much, and the cycle still stops. My next suspect was Memory limit, so I added:

ini_set('memory_limit','128M');

wp,

define('WP_MEMORY_LIMIT', '128M'); 

. - .

  • , ?

, script , .

EDIT ​​

script , scrap_slashdot() simplehtmldom.

, Wordpress, .

EDIT II @Allendar echo ini_get('memory_limit'); , , 128M..

+5
2

usleep(50000);, . PHP 5.4 :

ini_set('memory_limit', '32M'); // Force back to default X/W/L/M/AMP

function my_function(){
    $current = 0;

    foreach (range(100,999) as $art_id){ 
        $current++;
        outputProgress($current, $art_id );
    }
}

function outputProgress($current, $total) {
    echo "<span style='background:red;font-size:1.5em;color:#fff;'>" . $current .'/'. $total  . "% </span>";
    myFlush();
    usleep(50000);
}

function myFlush() {
    echo(str_repeat(' ', 256));
    if (@ob_get_contents()) {
        @ob_end_flush();
    }
    flush();
}

my_function();

echo memory_get_usage();

$current = 0;, , Xdebug.

282304 ( 275,69 ).

, 1- script .

ini_set('max_execution_time', 0);

.. , ;)

, script , , , . PHP- . , ( script ), ini , , PHP, "" .

+1

, "" script, - 16 650 (= 14 X 650 = 9100 , nvm, ), , , . , - . ( ) [( , php 5)]], ini_set(); ( -), memoery 63072 ( ~ 63kbs ~ 0.063mb > 128mb)

, $current? my_function() , error_reporting(E_ALL); ini_set('display_errors', '1');

-, , , Apache, .

0

All Articles