frank1 has asked for the wisdom of the Perl Monks concerning the following question:

Am having some problem with my script.

am calling it with cron job to run every some minutes

i recently received notice from my hosting that my script is causing problems to the server due to multiple requests per second

my question is that how can i loop using if statement because i have tested it and looks to be safe to use, because even using while loop it was skipping some condition if i say like

while (this eq to this) #then do this

it was skipping and just auto update to the database

working but sending mulitple requests per second on server and also sk +ipping some condition compering my $query = $dbh->prepare("SELECT plates FROM pintable WHERE status =? +"); $query->execute('OK'); while( my $row = $query->fetchrow_arrayref() ) { my $plates = $row->[0]; while ($STPU eq 'good') { # update DB with some data } $dbh->commit; } works great and not causing problems to server, but the problem it run +s single task per running my $query = $dbh->prepare("SELECT plates FROM pintable WHERE status =? +"); $query->execute('OK'); my $row = $query->fetchrow_arrayref(); my $plates = $row->[0]; if ($STPU eq 'good') { # update DB with some data } $dbh->commit;

Replies are listed 'Best First'.
Re: looping safely
by Corion (Patriarch) on Aug 19, 2023 at 17:46 UTC

    You should post an SSCCE instead of vague excerpts of what you think are the relevant parts of your program.

    In the problematic part of your program, you show:

    while ($STPU eq 'good') {

    So, when does $STPU ever get to ne 'good' ? Think about this.

    For your "slow" script, consider to do the database updates for more than one row maybe?

    Without seeing what takes the time, it's really hard to tell what goes wrong.

    My approach given the bad information you gave would be to loop up to 10 times in the "bad" program:

    my $count = 0; while ($STPU eq 'good' and $count++ < 10 ) { ... }

    This does a maximum of 10 changes.

    A reply falls below the community's threshold of quality. You may see it by logging in.