In loops with multiple things going on, I typically compute the earliest time to re-execute the thing:
my ($quit, $next_print, $next_foo) = (0, 0, 0); while (!$quit) { <<<do stuff>>> my $cur_time = time; if (time > $next_print) { print "...\n"; $next_print = time+5; # wait 5s for next print } if (time > $next_foo) { foo(...); $next_foo = time+7; # wait 7s for next foo() } <<<do more stuff>>> }
This way, print runs no more often than once per five seconds, irrespective of the speed of the stuff you're doing in your loop. A variation of this might be useful to you:
my ($quit, $next_req, $cur_time, $cnt) = (0,0,0,0); while (!$quit) { <<<do stuff>>> if ($cur_time<time) { # A new second has arrived, permit 3 more requests $cnt=3; $cur_time=time; } if ($cnt) { --$cnt; request(...); } <<<do more stuff>>> }
...roboticus
In reply to Re: do something no more than 3 times a second
by roboticus
in thread do something no more than 3 times a second
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |