in reply to Re^4: making a loop script with a remote URL call faster
in thread making a loop script with a remote URL call faster
For one possible interpretation:
use Time::HiRes (sleep time); my $next_time = time(); while (1) { next if time() < $next_time; $next_time += 60; # no accumulated lag my $price = fetch(); do_it($price); my $took = time()- ($next_time-60); handle_edge_case() if $took > 60; sleep 59 - $took; }
(totally untested, there are most probably dragons...)
The idea is that you only skip at max 59 seconds (or even a bit more) with sleep to let the loop catch the "exact time".
It still needs to handle the edge-case that fetch() and do_it() $took longer than 60 secs tho.
But how exactly really depends on the problem to solve...
So what am I missing justifying two communicating processes???
The logic might be clearer if I calculated $remain = $next_time - time(); instead of $took ... left as task for the interested reader :)
Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: making a loop script with a remote URL call faster
by cavac (Prior) on Jan 17, 2022 at 15:47 UTC | |
by LanX (Saint) on Jan 17, 2022 at 17:17 UTC |